Advertisement

Reading keys properly?

Started by February 03, 2000 08:08 AM
3 comments, last by Someone 24 years, 10 months ago
Hi! I''m writing a Win32 Application (without MFC) and have a slight problem with reading the keyboard input (not using DirectInput). OK! I basically have the program peeking at the messages and processing the messages like most Win32 tutorials suggest. I made an boolean array to store which key is currently down and which is up. When I receive a WM_KEYDOWN message I turn that key in the array to true and vice versa for a WM_KEYUP message. Then I run my main function that updates the scene and all and process according to the keys pressed by referring to the array of keys pressed. However, there is a problem when I press and release a key too fast. I suppose that it happens so fast that both the KEYDOWN and KEYUP message get processed before the update function is called. Therefore, the array gets turned to true (due to KEYDOWN) and back to false (due to KEYUP) before the update function, thus making it look like the key wasn''t pressed before. I suppose this problem should be worse if run on a slower system. I tried processing one message and updating the scene straight but causes rather jerky movements for just say when you move in a diagonal direction by pressing the UP and LEFT arrow keys. I tried using GetKeyState and GetAsyncKeyState but seems to be even slower in that sometimes the KEYUP isn''t even recognised. So, I''m wondering if anyone has a better way to handle this?
Instead of handling messages try to use GetKeyboardState() function to fill in the array. To avoid slowdowns maybe you can make another thread for reading from input.

Good luck

Advertisement
GetAsyncKeyState() should be fast enough if you dump the array. If I''m following you there''s a loop that checks every key you''re interesed in and then sets the array, and then your processing loop uses those results.

Assuming you only the the KEY_UP message to determine when the key isn''t down anymore, just check each GetAsyncKeyState() in the processing loop, the key is either down or not.

Just one of those cases of thinking too much.
-the logistical one-http://members.bellatlantic.net/~olsongt
I tried using GetKeyboardState and dump the data into a buffer, but it gives an error of "Can only be used in Win32 applications" or something. And I wasn''t even using MFC.

Actually, what I did was, whenever I receive a WM_KEYDOWN or WM_KEYUP message, I set ''Array[Key]'' to true or false. Thought this would reduce slowdown by reducing calls to GetAsyncKeyState...

Is it better to call GetAsyncKeyState for every single key I''m interested in? Wouldn''t that result in a little slowdown?
I decided to convert the format to using GetAsyncKeyState instead. The response is good, but there''s a problem. At times, if you hold 2 keys down at the same time and then release them at almost the same time, sometimes one of the keys is still recorded as down.

If I use HIBYTE(GetAsyncKeyState) to check for only the up/down state of the keys, the response isn''t that good any more. The problem is with the LOBYTE that remains set for the key, thus causing it to be recorded as true.

Anyone has any idea how to solve this?

This topic is closed to new replies.

Advertisement