• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Win32 and Virtual Keys

Adrian Tung

Golden Member
I'm having a slight problem with virtual keys and I'm hoping if someone knows the solution.

I have a program that saves keystrokes by storing their virtual key values in an INI file. I get the virtual key from the WM_KEYDOWN message, and apart from storing them, the name of the key itself is displayed to the user using GetKeyNameText. So if I hit the Space Bar it'll save VK_SPACE to the config file and at the same time display "Space" in the dialog box.

The problem occurs when I try to do this with the extended keys Insert, Home, Page Up, Delete, etc.

The way WM_KEYDOWN works, lets say I press Insert, the message will return VK_NUMPAD0 in wParam and set the flag at bit 24 on lParam specifying that it is an extended key. This also happens with the other extended keys (e.g. Home will return VK_NUMPAD7, PgUp will return VK_NUMPAD9, etc).

How can I translate this into the proper virtual key (i.e. VK_INSERT instead of VK_NUMPAD7) without explicitly going through a switch/case statement and doing it manually? Or is there a better way, or a function that I can use to get my desired results?


Hope this all makes sense and thanks in advance.


🙂atwl
 
From MSDN:

Most keyboards, the virtual-key codes VK_INSERT, VK_DELETE, VK_HOME, VK_END, VK_PRIOR, and VK_NEXT are generated on the numeric keypad only if NUMLOCK is off.

If a keyboard has a numeric pad, the numeric keys are frequently used as cursor-control and editing keys if NUMLOCK is off. If NUMLOCK is on, the virtual-key codes VK_NUMPAD0 through VK_NUMPAD9 are used for the digits.
 
Thanks for the quick response, singh, but I don't believe you've completely understood my question. Perhaps I should clarify:

Whether I hit the Insert key (or any of the other keys) on the 2x3 key matrix or on the Numpad, they both generate the same virtual key. The only way I can tell the difference is by bit 24 in lParam, which is set to 'on' if they key on the 2x3 key matrix was hit, and 'off' if the numpad was hit.

So I need a clean, proper way to do the translation rather than having to resort to the manual way:

if ( lParam & ( 1 << 24 ) )
{
switch ( (int)wParam )
{
case VK_NUMPAD0: iVirtKey = VK_INSERT; break;
case VK_NUMPAD1: iVirtKey = VK_END; break;
// etc ...
default: iVirtKey = (int)wParam;
}
}

... which is probably something I'll resort to if there's no cleaner way to do it. 🙁

FYI This program is going to be used as a "key settings" configuration program (similar to when you configure your keys for games), so telling the user to turn his Num Lock on/off is not an option, but something that needs to be taken into account.


Hope this puts it in a clearer perspective,
🙂atwl
 
BTW, some observations about the Numlock key:

Using the Insert key as an example,

With Numlock on, both Insert keys on the 2x3 matrix and on the Numpad return VK_NUMPAD0.

With Numlock off, the Insert key on the Numpad returns VK_INSERT properly, but the Insert Key on the 2x3 key matrix still returns VK_NUMPAD0.


🙂atwl


EDIT: Just for clarification, whenever I mentioned the 2x3 key matrix, I was referring to the cluster of keys Insert, Home, Pg Up, Delete, End and Pg Down which are arranged in a 2x3 matrix and are located above the arrow keys.
 
Back
Top