Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

SDL input

This topic is 4568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!