Skip to main content
gamedev.net
🔒 Locked

SDL input

Started by BloodLust666 6 replies 1k views
Original Post
BloodLust666
BloodLust666
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
Derakon
Derakon
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
BloodLust666
BloodLust666
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
Derakon
Derakon
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
BloodLust666
BloodLust666
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
Rob Loach
Rob Loach
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] [
Derakon
Derakon
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
owl
owl
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.