SDL input

Started by
6 comments, last by owl 17 years, 10 months ago
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..?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
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.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
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....
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
OHHH, that makes a lot more sense, thanks :)
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
Rob Loach [Website] [Projects] [Contact]
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.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
Instead of checking every frame, I would rather wait for input events and handle them only when they arrive (SDL_KEYPRESS /SDL_KEYRELEASE).
[size="2"]I like the Walrus best.

This topic is closed to new replies.

Advertisement