# Detecting A Keypress in WinAPI

## Recommended Posts

Fixxer    100
I am trying to make a header of defines that place a word in place of the keys code. I made a program that tells me the code of the key being pressed, but it gives me two different ones! 0X1E0001 Example: when I press "a", I get the code 0X1E0001 but if I hold it down for a few seconds, it gives me the code 0X401E0001. Which one do I use?

##### Share on other sites
Evil Steve    2017
What function are you using? GetAsyncKeyState()? If so, the value returned is a bitfield, not an actual value. I suspect that whatever function you're using is the same idea, it's not a value returned, it's a bitfield.

##### Share on other sites
Fixxer    100
I am using this code:

	case WM_CHAR:		{			char ascii_code = wparam;			unsigned int key_state = lparam;			char buffer[255];			// get graphics context			hdc = GetDC(hwnd);			// set foreground color to green			SetTextColor(hdc, RGB(0,255,0));			// set background color to black			SetBkColor(hdc, RGB(0,0,0));			// set color mode to opaque			SetBkMode(hdc, OPAQUE);			// print ascii code and key state			sprintf(buffer,"WM_CHAR: Character = %c ", ascii_code);			TextOut(hdc,0,0,buffer,strlen(buffer));			sprintf(buffer,"Key State = 0X%X ",key_state);			TextOut(hdc,0,16,buffer,strlen(buffer));			// release DC			ReleaseDC(hwnd,hdc);						// take what ever action		}break;

##### Share on other sites
Evil Steve    2017
From The MSDN:
Quote:
 lParamSpecifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.0-15Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.16-23Specifies the scan code. The value depends on the OEM.24Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.25-28Reserved; do not use.29Specifies the context code. The value is 1 if the ALT key is held down while the key is pressed; otherwise, the value is 0.30Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.31Specifies the transition state. The value is 1 if the key is being released, or it is 0 if the key is being pressed.

If you just want to know the ASCII code of the key, use the char in wParam. If you want to know any of the above information, test those bits in lParam.

##### Share on other sites
Fixxer    100
I am not sure what that means.

##### Share on other sites
Evil Steve    2017
Why do you care about the key state? What information do you need apart from the ASCII code of the key?

##### Share on other sites
Fixxer    100
I just need to detect if a key is being pressed for games.

##### Share on other sites
Evil Steve    2017
If you get a WM_CHAR notification, then the key is pressed. If you want to check from various points in your code, use GetAsyncKeyState(). Example:
bool IsKeyPressed(int nVirtKey){   return (GetAsyncKeyState(nVirtKey)&0x8000) ? true : false;}// Example usage:if(IsKeyPressed(VK_LSHIFT) && IsKeyPressed('A')){   // Left shift + A is pressed}

##### Share on other sites
Fixxer    100
I have this code from my book.

would this be ok?

code
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

example
KEYDOWN('A')

##### Share on other sites
Guest Anonymous Poster
Yes but you'll want to use VK_A

##### Share on other sites
Evil Steve    2017
Yup, that'll do the exact same. I'd prefer a function though, since they're "Better" than macros (Type safe, etc). An inline function will perform exactly the same as a macro.

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by Anonymous PosterYes but you'll want to use VK_A
No he won't, there's no such thing. For A to Z you use 'A' to 'Z'