Sign in to follow this  
Vento

Holding key states

Recommended Posts

Hello. I'm using SDL to handle keyboard events and I came to a conclusion that an array of 256 elements isn't enough to hold all keyboard keys states. I don't want to use SDL_GetKeyState() as it looks faster to handle key events manually. And coping all key states each time looks stupid for me. I wrote a code to print the value of key pressed (event.key.keysym.sym) and it returns various values. I can't find the system, so I gonna have to use SDL definitions or just get the number of symbol/key (whatever SDL defines) to be able to get the key state from the array. The problem is that I don't know how big array should I make. Or should I save key states some other way (have no idea how to do it without an array)?

Share this post


Link to post
Share on other sites
If your planning to store the state in an array of some sort, it only needs to be as big as the number of keys your going to track.

For example, if the only keys you care about are WSAD, then you only really need to every track those 4 key states. If no action occurs when the other keys are pressed, you can safely ignore them.

[edit]
I've also never used the SDL, but instead of copying the array all the time, you might pass by reference or by pointer, if possible.

Share this post


Link to post
Share on other sites
yes, but that would mean I have to set a value I gonna use in my array for each key. I just thought if I could use the sym code somehow. But if it's not possible, so... What ever, I gonna do what I should... Write lots of cases in handleEvent().

Because I've found such way to handle key states as making a structure with a name of function, key number/code and state. This would be a pain in the ass to change the state, but it would be pretty nice for changing the key binded.

P.S. How do professional programmers handle the key states? Of course they are using events (most APIs should give the access to them) and fill all the stuff manually, but how do they store? Do they use an array or do the use a structure for each key (as I said before)... I just want to know what is the best/fastest way to do this.

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