# Keypressing using Win32

## Recommended Posts

I am using the windows messages to get keyboard imput for my opengl application, but both the WM_CHAR and WM_KEYDOWN do not start repeating immediately so if you press a key and hold it, the message hits, waits about a second, then starts repeating at the rate which is precified in your windows settings (like the repeat rate when youre typing in MS Word). Is there a way to capture keys in a way more specific to the way we use them in games?

##### Share on other sites
A simple method is used.When a WM_KEYDOWN is recived the element in a bool array (which corresponds to the anscii value of the char pressed)is set to true and when a WM_KEYUP is recived then the element is set to false.

Now checking if a key is being pressed is quite simple,just check if the element of the bool array is true ;)

Hey's some source
//Keyboard I/O	int _keys;							// No of keys pressed	bool _key[256];						// Keys buffer//-----------------------Key(int ancii_key){	if(ancii_key>256)return false;	return _key[ancii_key];}//-----------------------WinMsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){			switch(uMsg)	{		case WM_CREATE:		{			break;		}			case WM_KEYDOWN:		{			if(_key[wParam] == false)_keys++;			_key[wParam] = true;			break;		}		case WM_KEYUP:              		{			if(_key[wParam] == true)_keys--;			_key[wParam] = false;			break;		}		case WM_CLOSE:		{			DestroyWindow(hwnd);			break;		}		case WM_DESTROY:		{			PostQuitMessage(0);			break;		}		default:		{			return DefWindowProc(hwnd, uMsg, wParam, lParam);		}	}	return 0;}

Helps?

##### Share on other sites
Yes there is. Look into GetAsyncKeyState() from the Win32 API.

##### Share on other sites
FireNet, Thanks for the insight on how peypressing works :-) I'll give that a shot tomorrow afternoon after study.

HansDampf, I tried that way and it does the same thing :-(

Edit: Also, with those methods I dont seem to be able to move diagonally?

##### Share on other sites
Quote:
 Original post by xyuriFireNet, Thanks for the insight on how peypressing works :-) I'll give that a shot tomorrow afternoon after study.HansDampf, I tried that way and it does the same thing :-(Edit: Also, with those methods I dont seem to be able to move diagonally?

Hi xyuri

HansDampf method has nothing to do with keypress - and therefore it did the same thing. When you want to know the state of a particular key you call GetAsyncKeyState(). It will tell you wether your key is pressed or not. You won't be able to achieve what you want just by using WM_CHAR or WM_KEYDOWN - except if you use FireNet's technique, which is essentially replacing GetAsyncKeyState() with a home technique.

Instead of handling the messages and answering to them, you use polling to choose what to do based on the informations you got. Therefore, to know if you have to move diagonnally, check the state of the 2 needed keys.

Hope it make sense,

##### Share on other sites
Directly from MSDN :
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getasynckeystate.asp)

Quote:
 GetAsyncKeyState FunctionThe GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.SyntaxSHORT GetAsyncKeyState( int vKey);Parameters vKey [in] Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes. Windows NT/2000/XP: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.Return Value If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks. Windows NT/2000/XP: The return value is zero for the following cases: * The current desktop is not the active desktop * The foreground thread belongs to another process and the desktop does not allow the hook or the journal record. Windows 95/98/Me: The return value is the global asynchronous key state for each virtual key. The system does not check which thread has the keyboard focus. Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.

So what you do is forget about keyboard messages and poll the keyboard with said function whenever in your game loop you feel like it. As you can poll as many keys as you want, you definitely can walk diagonally. Hope that helped.

##### Share on other sites
FireNet, I have re-read your code while fully awake, and I love how you have done it (That will change if it doesnt work for me ;))

WooHoo, a nice Lookup Tables web page :-) Bless the internet.

##### Share on other sites
WooHoo ! Thanks FireNet, your system works pretty kickass ! :-)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627698
• Total Posts
2978681

• 20
• 14
• 12
• 10
• 12