Advertisement

keypress event capture, how to?

Started by July 20, 2002 05:09 AM
2 comments, last by kanguru 22 years, 3 months ago
Im trying to learn a way to receive events from keyboard. Could someone tell me more about it. I''m currently using the keyevents capture that shown in NeHe''s OpenGL tutorial. It use a bool keys[256] array. And it will check like keys[VK_F1] or keys[VK_ESCAPE] to see if that key is pressed or not. What are codes for other keys? What people now ussualy do to capture keyboard events?
I''d just learn DirectInput. Microsoft has a tutorial contained in their SDK and there''s a few tuts here on gamedev. It doesn''t take long to learn unless perhaps your not familiar whatsoever with the architecture of DX then it may take a bit more effort.
Advertisement
Don''t start with DirectX. First of all, let''s analyze why NeHe''s method isn''t the most desirable:

1) It''s message-based. This can lead to lag (because of the nature of the way keyboard messages are sent and the message queue)
2)This means that you must write a handler block in your message procedure that does *something* - NeHe chooses to use an array representing the virtual keycodes. This can lead to less OO code. It''s also ugly
3) There are better methods.

You asked where to get listings of other keycodes. I suggest you read in the MSDN about virtual keycodes. You can do a search on things such as GetAsyncKeyState, WM_KEYUP, WM_CHAR, WM_KEYDOWN, etc. Each of these will link to a list of the VK_''s.

My engine uses GetAsyncKeyState. Some people would have severe problems with this, but most of them are too latched on to DirectInput to see its merit. I''ve only had two problems:
1) You can''t get buffered data efficiently.
2) You can''t access the scrollwheel of the mouse easily.

Most newbies who are just figuring out input don''t need either of the above, and a single function is easier to learn than an API, period. So, here''s some starter code.

inline bool KeyDown(int KeyCode){    return (GetAsyncKeyState(KeyCode) & 0x8000) ? true : false;}


That''ll return true if the specified key is down. Once again, you can get a list here. Remember that letters and number are not defined (i.e. there is no VK_F or VK_3), but you can make your own or simply pass the ASCII value. E.g.:
KeyDown(''A'');  // returns true if the a key is down


And there you have it. DirectInput might be something to look into for the future, but hopefully this is a bit easier to grasp, and it''ll suit most of your needs, at least until you become firm in your understanding of Win32.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

Thank you ZE it''s a great help to me

This topic is closed to new replies.

Advertisement