Sign in to follow this  
BloodLust666

SDL input

Recommended Posts

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..?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Instead of checking every frame, I would rather wait for input events and handle them only when they arrive (SDL_KEYPRESS /SDL_KEYRELEASE).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this