How can I handle keyboard event SDL ? (Advanced)

I'd like to make a keyboard handler but more advanced features like : Combo (Press consecutively) , Hold Key and Simple Key Pressing (1 Key 1 Action).

I should think of a way to implement this handler.Any idea ?
Key combos can get a little tricky I suppose. Basically all you have to do is keep track of the last N key presses (in the order in which they're pressed) and then examine it for known combos. You can do this by sticking them in a deque or list or something.

The other part is the timing. That is, only recognizing the combo if the user entered all the keys within the required time frame. (ie, if the user has to press Up,Left,Up within a span of 200 ms. This can be easily accomplished by taking a timestamp of when the key is pressed, and storing it alongside the key in your deque/vector/whatever. Once you recognize that the combo has been pressed, compare the timestamps of the first and last keys in the sequence and see if they are close enough together.


As for 'Hold Key' and 'Simple Key Pressing', those are pretty basic and I'm not sure how I can elaborate on them. If you want to see if a key is being held, you can check the realtime state. If you want to be notified when a key was just pressed, you can catch the key pressed event.
closed account (zwA4jE8b)
I am currently using SDL_GetKeystate(NULL) I find in some cases it is easier to work with rather than the event type.
also by using keystate you can check if multiple keys are pressed at the same time

1
2
if(keystate[SDLK_RCTRL] && keystate[SDLK_a] && keystate[SDLK_s] && keystate[SDLK_d] && keystate[SDLK_f])
  ....do something


the above will return true if "control a s d f " is pressed
Last edited on
Ah I thought he meant key combos like a fighting game or the Konami code.
Ok.now I got the picture in mind.If I want to check if user pressed 'A' key,must use SDLK_A. Do I have to do this for over 100 keys on the keyboard. ? That seems painful.
No, you can loop over whichever keys you want.
Topic archived. No new replies allowed.