Detect char keystrokes (up & down)

Started by
8 comments, last by Colin Jeanne 15 years, 3 months ago
I am trying to use the keys 'w' 'a' 's' and 'd' to move the character in my game. I use WM_KEYUP and WM_KEYDOWN for the cursor keys, but how to I detect when a character is pressed and released? Thanks, Nick
Advertisement
If you receive WM_KEYUP then you know that at some point it was down. So, WM_KEYUP should be what you're looking for. Characters dont have virtual-key codes like the arrow keys do (ie. there is no VK_W like there is a VK_LEFT); instead just use a capitalized character constant where you would use a virtual-key code.

For example, imagine here is your switch where you're handling virtual-keys and you want to handle 'w', 'a', 's', 'd' or the arrow keys for movement:

switch (keyCode) {   case 'W':   case VK_UP:      MoveUp();      break;   case 'A':   case VK_LEFT:      MoveLeft();      break;   case 'S':   case VK_DOWN:      MoveDown();      break;   case 'D':   case VK_RIGHT:      MoveRight();      break;}
sorry, I'm not using C#, just regular C++.
I don't have "keyCode", and WM_KEYUP and WM_KEYDOWN aren't triggered by the letter keys...and thats my problem
actually, you know what? KEYUP and KEYDOWN DO catch letters, but it is registering 'w' && 'W' both as 'W'...only capital...anyone know why that is? or if I can change it?
This might be a good time to post your actual code.
Why would you want to change it?
-----------------------------------------Everyboddy need someboddy!
someboddy: I guess thats a good point =D since I am only using it for motion. Someone also posted on another forum, he said I'm supposed to check for shift as well. Makes sense..lol
thanks guys
Quote:Original post by CPPNick
actually, you know what? KEYUP and KEYDOWN DO catch letters, but it is registering 'w' && 'W' both as 'W'...only capital...anyone know why that is? or if I can change it?


WM_KEYDOWN and WM_KEYUP notify you when a key on your keyboard is pressed/released. There is no "w" and "W" key, just one: "W". WM_CHAR uses the state of the rest of the keyboard to differentiate between "w" and "W" (i.e. when shift is pressed, it's "W"; when it's not pressed, it's "w").
Up and down (WM_ commands) have to be overridden. If you are using Visual Studio simply pick your main class and add the routine. WM_ commands are not sent through the keyboard routine, they are the keyboard routines (just like WM_MOUSEMOVE uses OnMouseMove()).

If you Translate and Dispatch you can capture those commands before the need to go to a routine.

******************************************************************************************
Youtube Channel

Quote:Original post by CPPNick
actually, you know what? KEYUP and KEYDOWN DO catch letters, but it is registering 'w' && 'W' both as 'W'...only capital...anyone know why that is? or if I can change it?

'W' is the key. 'Shift+W' is a key combination. I'm not familiar with the C# implementation of these events but there is probably something to tell you if the Shift key is also pressed (as well as the state of Ctrl and Alt).

This topic is closed to new replies.

Advertisement