Sign in to follow this  

Keystroke detection..

This topic is 4867 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 know, I know, a pretty newbie-ish question. But ya, how does one detect specific keystrokes, such as the up arrow key? If at all possible, could someone point me to somewhere that details how to detect all the keys? My thanks in advance! P.S, I just finished my first working program! Hurray for temperature converters! :þ

Share this post


Link to post
Share on other sites
ok well what are u doing this for and what language?

if you are using C/C++ and Win32 look up WM_KEYDOWN/UP for WndProc, MFC has these messages too but through a different interface (message map), you could also try DirectInput (part of DirectX). If you are using C#, VB.NET then most things have a keyPress/Up/Down message handlers.

Share this post


Link to post
Share on other sites
You can watch the WM_KEYDOWN / WM_KEYUP messages within your WndProc if you want notification of when the event occurred. This is useful as you can record some timing information and react instantly to the event should you need it.

If you just want to know if a key is pressed, do as Jedite said and use GetAsyncKeyState with the required VKEY (eg: VKEY_UP).

As nts said before, a lower level version is available for games in the form of DirectInput (you can also monitor mouse and joystick events too). With DirectInput, there's a lot more setting up involved, but you get better speed and flexibility with it because you're not hanging onto the Win32 API. If you don't need that sort of power, the previous two methods should more than suffice.

Share this post


Link to post
Share on other sites
One way of storing the information gained from the WM_KEYUP / WM_KEYDOWN events is to use a boolean array. I used this for one of my programs. here's a breif example:
bool KeyDown[256];

; ...

case WM_KEYDOWN:
KeyDown[(int)lparam] = true;
break;

case WM_KEYUP:
KeyDown[(int)lparam] = false;
break;
Not the only way of doing things, but a fairly tidy way to keep track of keyboard input.

Share this post


Link to post
Share on other sites
You can also use the function GetAsyncKeyState(key code). So to test if a key is down, you could do this. This can be called anywhere in your program


// the 0x8000 mask out some data that you don't want
if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)DoStuff()


Share this post


Link to post
Share on other sites

This topic is 4867 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this