Handling User Input/Pressing but not releasing a key

Started by
4 comments, last by Phillemann 20 years, 2 months ago
Hi The topic might be a bit confusing ... I''m getting into OpenGL right now and have started on a simple tetris clone (nice stuff for the beginner ). The basic framework is done and now it comes to user input. NeHe uses this ''bool keys[256]'' array which basically works fine, but I always have to create a counter that prevents the key action (move the current block right, left, rotate etc.) from executing every frame. It would be better if the action just executes once, when the key is pressed. My goal is to get away with as less Windows stuff as possible and wonder how this is usually done in games. I have thought of a (second) event stack which works like this: When an event occurs, it is placed on top of a stack (this happens in WndProc), so I can iterate over the stack, execute the according actions (move, quit etc.) and delete it later, in my gameLogic() function. As this seems a little complicated there might be a simpler solution, so could you give me an advice?
Advertisement
Bit 30 of the lParam parameter of the WM_KEYDOWN message is a flag indicating the previous state of the key. You could query that to determine if the key is being held down and only populate the keys[] array if the key was not previously down.

Another method is to maintain a separate oldkeys array. Compare keys[] to oldkeys[] to determine state changes. Then copy keys to oldkeys to prepare for the next frame.
yeah, or have a char key[256], and have flags for each key, stating if it''s up, down, toggled, debounced, ect... you can even have a structure CKey Key[256], and store key states in a structure, like how long it''s been up, down, double klicks, ect...

Everything is better with Metal.

For now I''ve solved the problem using the second array keysprev[256] (which works perfectly fine). When it gets more complex I will think about the method oliii suggested (using a structure containing more data about the key).
Anyway, thanks for your quick reply
hm, i guess id go with the char array. as far as i can tell it seems that bools are byte and not bits anyway (might depend on your platform), so theres some wasted space.

#define KEY_RELEASED -1
#define KEY_UP 0
#define KEY_PRESSED 1
#define KEY_DOWN 2

inline void JSAInlGetNewState(unsigned key, unsigned char newState) {
if (newState && Keys[key]<=0) Keys[key]=KEY_PRESSED;
else if (newState && Keys[key]>0) Keys[key]=KEY_DOWN;
else if (!newState && Keys[key]>0) Keys[key]=KEY_RELEASED;
else if (!newState && Keys[key]<=0) Keys[key]=KEY_UP;
}
f@dzhttp://festini.device-zero.de
I had the same issue when developing a solution of my own, currently i''m just using dual flag modes where i simply check if it was not pressed the last frame and using the immediate mode dinput stuffs (this sounds like what you''re doing too).

I believe you can use buffered input however to determine when the key is pressed or lifted in DX or use it to do something similar anyway. This is what i was going to do when i get around to rebuilding some of the code as i move my game from ''just get the darn thing goin'' to ''get it working well''.

I hope this helps you anyway

Lorenz (krysole) Pretterhofer
sleep, caffeine for the weak minded

This topic is closed to new replies.

Advertisement