{"content":"How can I distinguish between the numeric keypad 0 and the top-row 0 in the WM_CHAR message?\n\nLast time, we looked at how to distinguish the numeric keypad 0 and the top-row 0 in the WM_KEY­DOWN message. We may as well look at the analogous table for WM_CHAR.\n\nEvent wParam Extended? Numpad0 with NumLock on VK_0 0 Numpad0 with NumLock off (no WM_CHAR) Ins key (no WM_CHAR) 0 on top row VK_0 0\n\nI got the name VK_0 from this comment block in winuser.h.\n\n/*\r\n * VK_0 - VK_9 are the same as ASCII '0' - '9' (0x30 - 0x39)\r\n * 0x3A - 0x40 : unassigned\r\n * VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)\r\n */\r\n\nUh-oh. The extended bit doesn’t distinguish between the two. They both show up as VK_0, non-extended.\n\nWhat changes is something not in the above table: The scan code.\n\nSo let’s convert the scan code back to a virtual key.\n\nauto vk_from_scan = MapVirtualKey((lParam >> 16) & 0xFF, MAPVK_VSC_TO_VK);\r\n\nEvent wParam Extended? vk_from_scan Numpad0 with NumLock on VK_0 0 VK_INSERT Numpad0 with NumLock off (no WM_CHAR) Ins key (no WM_CHAR) 0 on top row VK_0 0 VK_0\n\nSo we can infer which zero was pressed by taking the scan code, mapping it to a virtual key, and seeing whether it’s the Ins key (from the numeric keypad) or the 0 key (from the top row).\n\nBut wait, we’re not done yet.\n\nThere are ways to type the character 0 without using the numeric keypad or the top row. For example, you can hold the Alt key and then type 4,8 on the numeric keypad, and that will type a 0. I tried it out, and the vk_from_scan was VK_MENU, which is the virtual key code for the Alt key. Another way of entering a 0 is by using an input method editor (IME). Or there might be a custom keyboard layout that generates a 0 through some wacky chord sequence.\n\nTherefore, if the vk_from_scan is neither VK_INSERT nor VK_0, you have to conclude that the 0 was entered by some means other than the numeric keypad or the top row.\n\nThe post How can I distinguish between the numeric keypad 0 and the top-row 0 in the WM_CHAR message? appeared first on The Old New Thing.","contentType":"text/plain;utf-8","attachments":[],"quotePin":""}