Jump to content
  • Advertisement
Sign in to follow this  
CPPNick

Detect char keystrokes (up & down)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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").

Share this post


Link to post
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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!