Archived

This topic is now archived and is closed to further replies.

handling keyboard input

This topic is 5014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites