• Advertisement
Sign in to follow this  

Key Presses vs Key States

This topic is 3934 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

which is more efficent, checking for key states and key presses? btw i'm working through lazyfoo's SDL tutorials and it shows both, which is why i'm asking

Share this post


Link to post
Share on other sites
Advertisement
If you read the first part of his Key State tutorial, you would have seen:
Quote:
Sometimes just checking is a key is up or down is easier than monitoring events.

This tutorial will teach you to check if the key is pressed, rather than monitoring key events.

From what I've seen (I haven't used SDL before) "Key States" works on a buffer, while the tutorial "Key Presses" uses event handlers and all that.
I'd suggest key buffers for making a game, since you only want to buffer key presses once per frame, rather than directly checking it at each instruction (well, each 'key' conditional), so you have somewhat uniform results.
Plus, if you create a buffer to store a previous key state, you can check if a user just pressed the key down (!prev && cur), is holding it (prev && cur), and just released it (prev && !cur).

EDIT: Didn't read the part on efficiency... I'm not sure how exactly they work, so I can't say. However, your best choice is probably buffers (Key States).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement