SDL input
I have a class that does all the input. I have a KeyState and, just added, KeyPressed, which stores the Key that was JUST pressed that frame. How would I make it so when a key is pressed the Keypressed and KeyState is true but while the key is STILL being pressed, the KeyState is still true but the KeyPressed is false..?
Just set KeyPressed to false if KeyState is true and you didn't set KeyPressed to true that frame. That does mean that you'll need at least one frame of non-pressed time before KeyPressed can be true again, though.
but won't i need another flag to keep track if Pressed was set to true while it's being pressed down. but that way, i'll get an effect as if it was rapidly being pressed.
ex:
frame 1:
Press K key
State = true;
if (pressed == false)
Pressed = true;
else
Pressed = false;
frame 2:
state = true;
if (pressed == false)
Pressed = true;
else
Pressed = false;
frame 3:
state = true;
if (pressed == false)
Pressed = true;
else
Pressed = false;
etc....
ex:
frame 1:
Press K key
State = true;
if (pressed == false)
Pressed = true;
else
Pressed = false;
frame 2:
state = true;
if (pressed == false)
Pressed = true;
else
Pressed = false;
frame 3:
state = true;
if (pressed == false)
Pressed = true;
else
Pressed = false;
etc....
You said you already had two booleans - KeyState, and KeyPressed. So why wouldn't this work?
Frame 1:
press K key
if (!KeyState) then KeyPressed = true
else KeyPressed = false; // sets KeyPressed to true
KeyState = true;
Frame 2:
if (!KeyState) then KeyPressed = true
else KeyPressed = false; // sets KeyPressed to false
KeyState = true
Frame 3:
et cetera...
Frame N:
let up on K key
KeyPressed is still false;
KeyState = false;
Edit: the trick is to set KeyPressed before you set KeyState.
Frame 1:
press K key
if (!KeyState) then KeyPressed = true
else KeyPressed = false; // sets KeyPressed to true
KeyState = true;
Frame 2:
if (!KeyState) then KeyPressed = true
else KeyPressed = false; // sets KeyPressed to false
KeyState = true
Frame 3:
et cetera...
Frame N:
let up on K key
KeyPressed is still false;
KeyState = false;
Edit: the trick is to set KeyPressed before you set KeyState.
You could also have an array of booleans saying which buttons are being pushed down at any time. When the button is pressed and the event is called, set KeyStates[KEY] to true. When the key is released set KeyStates[KEY] to false. If you want to check whether a key is being pushed, just check if KeyState[KEY] is true or false. Using this method will allow you to have more then just three keys down at any given time.
Incidentally, for a good solution to controller woes, I strongly recommend checking out VControl, the Virtual Controller Library. I'm using it for my game and it was quite easy to set up. It also neatly abstracts away from the keyboard "controller" system and gives you a handy-dandy config file to set controls in.
Instead of checking every frame, I would rather wait for input events and handle them only when they arrive (SDL_KEYPRESS /SDL_KEYRELEASE).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement