Skip to main content
GameDev.net gamedev.net
🔒 Locked

Holding key states

Started by Vento Dec 22, 2009 at 2:17 PM 1 replies 1k views
Original Post
Vento
Vento
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)?
Rattrap
Rattrap
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.
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
Vento
Vento
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.