GetKeyNameText numpad missing text
Asked Answered
H

1

6

I'm trying to get the name of a pressed key with GetKeyNameText, using the make/scan code and extended-key flag given by raw input:

std::wstring GetKeyName(const RAWKEYBOARD& info)
{
    WCHAR n[128];
    const int l = GetKeyNameTextW((info.MakeCode << 16) | ((info.Flags & RI_KEY_E0) != 0 ? 1 << 24 : 0), n, ARRAYSIZE(n));
    if(l == 0) { return L""; }
    return std::wstring(n);
}

This works well for most keys, however, Num / (scan code 53) and Num * (scan code 55) give wrong results: They both give the string " (ZEHNERTASTATUR)" (German keyboard layout, so would be "Num " in English) – so the / or * is missing in the name.

I've tested this with two different keyboards, with same results (using Windows 10 btw), am I missing something? Why do these two keys not have the correct names?

Horick answered 8/11, 2019 at 4:26 Comment(1)
Actually, it works in English (and other languages). In English it correctly returns Num *. Only German seems affected.Cappello
S
4

It is a bug in Windows that existed for ages and still exists.

In this article by M. Kaplan from 2012 he explains the messy state of access to key names in Windows. There doesn't seem to be a lot of improvement on that front though.

The key names are stored in the compiled keyboard layout files which are DLLs, in their .data section in a proprietary format exported as KbdLayerDescriptor. The German keyboard layout is stored in C:\Windows\System32\KBDGR.DLL. If you use the strings utility on it, you will get this (yes the order is weird):

...
NACH-LINKS
^ZIRKUMFLEX
NACH-RECHTS
STRG-RECHTS
LINKE WINDOWS
NUM-FESTSTELL
RECHTE WINDOWS
BILD-NACH-OBEN
BILD-NACH-UNTEN
UMSCHALT RECHTS
ROLLEN-FESTSTELL
 (ZEHNERTASTATUR)      <<<<<< THE ISSUE EXISTS
 (ZEHNERTASTATUR)      <<<<<< HERE AS WELL!
0 (ZEHNERTASTATUR)
3 (ZEHNERTASTATUR)
2 (ZEHNERTASTATUR)
1 (ZEHNERTASTATUR)
+ (ZEHNERTASTATUR)
6 (ZEHNERTASTATUR)
5 (ZEHNERTASTATUR)
4 (ZEHNERTASTATUR)
...

We can verify that this is not a bug in how strings' heuristics determine the start and the end of the string by looking at it in a hex editor:

enter image description here

As you can see there is no * or / existing in the file at all.

So it seems you are stuck with the bad names Windows gives you. (There are also bugs in other languages by the way.) Even the Windows shell itself exhibits this problem, for example this is what happens when I open the properties of a shortcut and attempt to set the hotkey to Num *:

enter image description here

I'm afraid you either have to put up with it or maintain your own list of fixes to apply to the broken info you get, or your own separate list of key names entirely, with corresponding translations... This is what other applications seem to do, VS Code for example.

(Of course if this affects only you or a small number of people, you could go to the lengths of creating a custom keyboard layout using the Microsoft Keyboard Layout Creator and set the name there correctly, but this may cause too much collateral damage (e.g. Microsoft account settings syncing issues) to be worth it.)

Smalley answered 14/7, 2020 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.