Skip to main content
gamedev.net
🔒 Locked

Virtual Key Codes

Started by Ranthalion Jul 17, 2003 at 9:51 AM 4 replies 1.1k views
Original Post
Ranthalion
Ranthalion
I wonder if anyone can tell me why I''m getting an error when I compile this code.
__declspec(dllexport) LRESULT CALLBACK KeyEvent(int nCode,
												WPARAM wParam,
												LPARAM lParam)
{
	if(nCode==HC_ACTION)
	{
		KBDLLHOOKSTRUCT *hooked=(KBDLLHOOKSTRUCT *)lParam;

		// Disable ATL+TAB 

		if (hooked->vkCode == VK_TAB && hooked->flags & LLKHF_ALTDOWN) 
			return 1; 
		// Disable ALT+ESC 

		if (hooked->vkCode == VK_ESCAPE && hooked->flags & LLKHF_ALTDOWN) 
			return 1; 
		// Disable the WINDOWS key 

		if (hooked->vkCode == VK_LWIN || hooked->vkCode == VK_RWIN) 
			return 1; 
		if (hooked->vkCode==VK_L && hooked->flags & LLKHF_ALTDOWN)  //<---This is where the problem is.

			MessageBox(NULL,"It works", "Does it work?", MB_OK);

		//Add processing for alt+L for accessing password prompt

	}
	return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
}
[source]

Is VK_L not a defined Virtual Key Code?  If I replace it with 0x4C, then it is fine, but that is unreadable if I''m looking over it next week you know...
sbennett
sbennett
If you''re trying to handle an event for the ''L'' key on the keyboard, try

if (hooked->vkCode==''L'' && hooked->flags & LLKHF_ALTDOWN) //<---This is where the problem is.

MessageBox(NULL,"It works", "Does it work?", MB_OK);


The ASCII values for letters and digits work as virtual key codes, AFAIK.
S1CA
S1CA
Its because VK_L isn''t defined in the Windows headers, only in the docs. The VK_* codes that are defined are in the WinUser.h header file.

Locations on the keyboard for alpha-numeric keys DO change depending on the keyboard language anyway for example a German keyboard swaps the Z and Y keys around when compared to a UK or US keyboard.

This means that any static definition for the virtual key code of an alphabetic character in a header file would only make sense on one locale of keyboard.

I think MS explicitly don''t define the symbols for alphanumeric keys to stop people from assuming that the virtual key codes actually match up to a particular alphabet: THEY DON''T!

If there was a "#define VK_Y blah", then pressing the Z key on a German keyboard would return VK_Y and vice versa.

All you can rely on is "user has pressed the 0x46 key" - don''t assume that key has a name (unlike the special keys like escape).

--
Simon O''Connor
ex -Creative Asylum
Programmer &
Microsoft MVP
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Yanroy
Yanroy
I thought the whole point of virtual key codes was to avoid the very problem just mentioned...

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
sbennett
sbennett
quote:
Original post by Yanroy
I thought the whole point of virtual key codes was to avoid the very problem just mentioned...



Yep. Virtual key code != Keyboard scan code.
S1CA
S1CA
quote:
Original post by sbennett
quote:
Original post by Yanroy
I thought the whole point of virtual key codes was to avoid the very problem just mentioned...



Yep. Virtual key code != Keyboard scan code.


Agreed: Virtual key code != Keyboard scan code.
Likewise: Virtual key code != ASCII character code


From MSDN:
quote:
Keystroke messages provide a lot of information about keystrokes, but they don''t provide character codes for character keystrokes. To retrieve character codes, an application must include the TranslateMessage function in its thread message loop. TranslateMessage passes a WM_KEYDOWN or WM_SYSKEYDOWN message to the keyboard layout. The layout examines the message''s virtual-key code and, if it corresponds to a character key, provides the character code equivalent (taking into account the state of the SHIFT and CAPS LOCK keys).



E&OE: I''m having a pretty bad week so far and I''m in serious need of sleep...
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.