handling keyboard input

Started by
7 comments, last by ehmdjii 20 years, 1 month ago
hi, i''m coding in C++ and plain WinAPI. my problem is that when i press a key, it gives me the key-message all the time. for example im using "w" to switch between wireframe mode and normal polygon mode. but when i press w the function gets called all the time while w is down. and also if its only hit shortly the function gets called about 100 times. the second problem is, that im having a windows-dialog above my openGL window. and when this dialog is active all the keys dont work anymore. here is my code: while(!done) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message==WM_QUIT) { done=TRUE; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { if (active) { if (keys[''R'']) { H.create(); H.iterate(it); } if (keys[''W'']) { wireframe=!wireframe; } else { DrawGLScene(); SwapBuffers(hDC); } } } } www.embege.com
Advertisement
one way around this is to store the previous keystate as well (in another array). when a key is pressed, make that entry in your "keys" array TRUE, as you were doing. then, each iteration of the main loop, copy the "keys" array into the other array ("keys_last" or somesuch). only perform the operation when the "keys" array says that key is TRUE, and the "keys_last" array is FALSE.

does that make sense?
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
well yes that worked! but is this also the method all you elite gamecoders use?


what about the second problem?

thanks!

www.embege.com
Yup, I used the One-Shot method like Krez, the other way I can think of is to actually fire the function on the key going up (but that depends on how your code works, and what your function is.

Advantade of the One-Shot method is if you have weapons (say) with variable rates of fire, you can run a timer on the variable set, and reset it every n miliseconds to give automated fire while the key is pressed.

Bp
i actually use an array of structs:
struct SKeyState  {  bool            KeyDown;  bool            Last;  unsigned long   Flags;  }; 


with "Flags" storing random stuff (whether shift, control, alt were held down).

for your second problem, i''m guessing that the dialog has focus, so IT gets the keypresses.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
quote:Original post by krez
for your second problem, i''m guessing that the dialog has focus, so IT gets the keypresses.


ok, so how can i get the keypresses from the dialog as well?

thank you!

www.embege.com
you could catch the keypresses in the dialog wndproc, and PostMessage() or SendMessage() it to the main window.

there might be a better way (i''ve never actually done that), but that is probably the simplest.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
but in the main - loop it isnt handled as keypresses.

its just the code i posted above.
i am assuming this code is based off NeHe''s stuff (that''s what it looks like to me anyway).

the "keys" array has its elements set to TRUE in the WM_KEYDOWN case in your wndproc. whether the main window has focus and gets a keypress, or you send one manually (say, from the dialog''s wndproc), the main window''s wndproc handles it (that''s what the "DispatchMessage" in your main loop does).
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

This topic is closed to new replies.

Advertisement