Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetKeyNameText numpad missing text

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?

like image 781
Bizzarrus Avatar asked Nov 08 '19 04:11

Bizzarrus


People also ask

How to get the numpad on a Mac keyboard?

Note: Sometimes, when you press these keys, you get the on-screen keyboard, but you don’t get to see the numeric pad. So, to get the Numpad, all you have to do is press the “ options ” button and click on the check box “ turn on the numeric keypad, ” and apply “Ok,” and after that, you get the Numpad on the top of the on-screen keyboard.

What is the getkeynametext alias?

The winuser.h header defines GetKeyNameText as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors.

How do I get the name of a character key?

The name of a character key is the character itself. The names of dead keys are spelled out in full. The winuser.h header defines GetKeyNameText as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant.

How to add special symbols on a numpad?

Also, you can add special symbols by using the alt codes that are even not available on the actual Numpad. On the other hand, if you don’t need any customization on the top of the virtual Numpad, you can use the number pad using the on-screen keyboard. 4. Buy an External Numpad


1 Answers

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.)

like image 89
CherryDT Avatar answered Oct 21 '22 01:10

CherryDT