On release and on press\click on same button


#1

Is it possible to have both interacting on the same key smoothly? let me explain, say I have an attack command, if press once to attack, character should do “whatever” , however if I hold the key the character should delay the attack until release…I have to define a timing mechanism to achieve this right?, what is the best approach to it so as to avoid conflict or input feeling unresponsive

thanks


#2

i dont know exactly what you want to do, but from what you write i get that all you want to do is listen to key release and trigger a command. basically regardless of when the key was first pressed down, you first trigger the action when the key is released again.

on the other hand you might want to also store the time when the key was pressed, to increase power as long as the key is pressed or show some indicator on screen or something. still you dont need to really define a timing mechanism, all you would need to do is, if the key is currently pressed, take the current time, see how much time passed since the time you stored when the key was pressed and use the resulting time period for your calculations, still you woulf first trigger the action once the key is released and then take the current time to calculate the duration since key was pressed down if you need that as factor


#3

Something like this.

Edit: I missed a step. Corrected.


private float timeTaken = 0;
private float maxTime = 3.0;
boolean buttonPressed = false;

@Override
public void onAction(String name, boolean isPressed, float tpf) {

    if (name.equals("myButton") {
        buttonPressed = isPressed;
    }
        
}

@Override
public void update(float tpf) {

    if (buttonPressed) {
        timeTaken += tpf;
    }
    else {
        timeTaken = 0;
    }
    
    if (timeTaken >= maxTime) {
        doSomething();
    }
}


#4

for clarification, In a shooter you want that when you tap your key a shot is fired instantly or in the shortest possible time. similarly I want that if TAP the e.g. melee key my action to appear to be triggered instantly or in the shortest possible time, but if hold down the key for the action to wait until release to be triggered…basically I want to define “shortest possible time to be considered a key tap”.

edit took too long thanks @jayfella think that’s it will take a look


#5

well all you can do is push the button down and release it again and to release it you have to push it down first
so you either fire the action upon key pushed down, then there is no way to delay it (well ofc you could just delay it by any time)
or you fire it upon key release where you could take the duration into account.
the thing is, how would you determine someone wants to fire the action immediately when they hold down the key still? you would have to assume they want to charge it

you also could use some sort of modifier like if ctrl is pressed, fire the action as soon as button down is triggered, but if ctrl is not pressed, trigger the action upon key release first

i still dont understand “shortest possible time to be considered a key tap”, whenever a key release follows a key down i would consider this a key tap no matter how fast it happens