
[ad_1]
The approach I course of keys presses in my functions is I’ve 2 lists starting from 0-127 (all ASCII keys). One is for a key_duration_state
and the opposite for a key_hold_state
. I do not know what kind of occasion listener you are utilizing, however take a generic onKeyDown/Up(int key)
listener operate for instance. This capabilities job is to let you already know when a key has been pressed, the place key
was the worth of the important thing pressed. I’ve it return 1
so long as the secret’s nonetheless pressed, and 0
when it is launched. All I want in an effort to register each ASCII keys press/launch state is cross my record to the operate. Same goes for length. Just increment that keys duration_state so long as its pressed. Then reset it to zero upon launch:
// tells if its pressed
bool onKeyDown(int key) {
key_hold_state[key] = 1;
key_duration_state[key]++;
}
// tells if its launched
bool onKeyUp(int key) {
key_hold_state[key] = 0;
key_duration_state[key] = 0;
}
What I do to find out if a secret’s being held for a length is use my record key_hold_state
which shops whether or not the secret’s pressed down or not. Then I’ve one other operate, say isHeldFor(int length)
which checks if that key was held for the length:
bool wasHeldFor1s = keyListener.isHeldFor('A', DURATION_1s);
bool isHeldFor(int key, int length) {
return duration_held_state[key] == length);
}
This is simply my method. There might be a greater approach, however storing each keys states into lists is an effective transfer. Keeps it clear, concise, and arranged.
[ad_2]