Archived

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

user01

Keyboard

Recommended Posts

I was using nehe base code to write a viewer, and no move the objects I was using keyboard controls. When I push down on the key and hold it, their is a delay before the code is executed. Then, if I continue holding it, the code I wrote for the key will begin to execute more frequently (at a rate that I would expect). I am assuming that this must have something to do with windows proc, but otherwise I don''t know how to have the key trigger as soon as it is touched. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Yup yup. Stock windows msg handling is not the fastest thing in the world though in demo situations it works okay. Keep in mind that windows will send the key down msg as long as the keyboard is sending them which is as long as you hold down on the key.

The solution to your problem is to ignore the direct windows api for input handling and turn to direct input or sdl instead. Both api''s use manual polling which takes place as fast as your program loop can handle it as opposed to when in the windows event queue your program finally gets to it.

hope this helps some

if anyone thinks I''m wrong here, feel free to correct me. I''m still learning too...

8^)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look up GetAsyncKeyState() it''s part of the Win32 API and is asyncronous so it has very little lag. It''s very simple to use for keys/buttons. Good luck.

Share this post


Link to post
Share on other sites
You can use the DirectInput API''s, GetAsyncKeyState(), or a common and simpler method of defining a bool array to hold key states. Windows will send a WM_KEYDOWN message when a key is pressed (and keep sending it), and sends a WM_KEYUP message when it''s released. Use that to store states.


// Assume a global is defined

bool g_bKeyStates[256];

// and in your window procedure

// the key messages pass the virtual keycode of the key in wParam

case WM_KEYDOWN:
g_bKeyStates[wParam] = true;
break; // or return, depending on how you setup the function


case WM_KEYUP:
g_bKeyStates[wParam] = false;
break;

// Then for testing in code, it''s simply

if (g_bKeyStates[VK_SPACE])
// do something


// Look up the VK_ values for virtual keycodes.

// Not all are defined for every character, like the letters, so

// you''ll have to use the character itself, for instance:

if (g_bKeyStates[''A'']) // If ''a'' is pressed

// do something




Hope that helps.

Share this post


Link to post
Share on other sites
Thanks alot guys. I havent gotten a chance to fully implement it yet, but GetAsyncKeyState seems to be working like a charm. I think it will work nicely.

Share this post


Link to post
Share on other sites