# Detect char keystrokes (up & down)

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

## Recommended Posts

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

##### Share on other sites
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;}

##### Share on other sites
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

##### Share on other sites
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?

##### Share on other sites
This might be a good time to post your actual code.

##### Share on other sites
Why would you want to change it?

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by CPPNickactually, 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").

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by CPPNickactually, 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).