Reputation: 210745
Is there a way to programmatically find out what kind of keyboard a computer has (i.e. where keys are located, and what extra keys are present in which locations)?
A little error is acceptable, if the keyboard is very non-standard, but in general, the point is to construct an on-screen keyboard-like application that can dynamically draw the keyboard layout on the screen, with high accuracy.
Upvotes: 5
Views: 5150
Reputation: 1441
Update from 2023
There is a new KEYBOARD_EXTENDED_ATTRIBUTES struct and IOCTL_KEYBOARD_QUERY_EXTENDED_ATTRIBUTES IOCTL added in Windows 10 that can return additional keyboard info.
See "15.18 Descriptive Controls" in the recent HID Usage Tables spec. These was from adopted HID Usage Table Review Request 42: Consumer Page Keyboard Assist Controls from 2013.
bool QueryInfo(const ScopedHandle& interfaceHandle)
{
// https://docs.microsoft.com/windows/win32/api/ntddkbd/ns-ntddkbd-keyboard_extended_attributes
KEYBOARD_EXTENDED_ATTRIBUTES extended_attributes{ KEYBOARD_EXTENDED_ATTRIBUTES_STRUCT_VERSION_1 };
DWORD len = 0;
if (!DeviceIoControl(interfaceHandle.get(), IOCTL_KEYBOARD_QUERY_EXTENDED_ATTRIBUTES, nullptr, 0, &extended_attributes, sizeof(extended_attributes), &len, nullptr))
return false;
DCHECK_EQ(len, sizeof(extended_attributes));
FormFactor = extended_attributes.FormFactor;
KeyType = extended_attributes.IETFLanguageTagIndex;
PhysicalLayout = extended_attributes.PhysicalLayout;
VendorSpecificPhysicalLayout = extended_attributes.VendorSpecificPhysicalLayout;
IETFLanguageTagIndex = extended_attributes.IETFLanguageTagIndex;
ImplementedInputAssistControls = extended_attributes.ImplementedInputAssistControls;
return true;
}
But seems these is not adopted by vendors and my modern Logitech MX Keys
keyboard returns zero values in these fields..
Upvotes: 2
Reputation: 23303
when connected to a computer, keyboards sends "scan codes" to the operating system. on windows, scan codes are then converted into virtual keys (a hardware independent mapping of the keyboard) then to real characters.
the MapVirtualKeyEx()
function of the windows API allows you to translate between scan codes, virtual keys and characters. it should also be able to tell you if a key is non-existing.
together with GetKeyboardLayout()
which tells you which keybaord is active at any point in time (keyboard layout can be different for different running applications), it should allow you to build a pretty accurate map of the keyboard.
anyway, have a look at the keyboard input section of the MSDN
i will add that all keyboards have almost the same layout. although there is no way to know where a key is physically located, you can probably guess from the scan code and basic knowledge of your own keyboards.
Upvotes: 5
Reputation: 942177
There is no mechanism by which a keyboard can tell Windows what its physical layout looks like. Easy to see with the Windows version of an on-screen keyboard, osk.exe. It seems to be able to guess the form-factor of the machine (laptop vs desktop) but on my laptop it doesn't match the layout of the keyboard.
Use the osk.exe layout as a template so nobody can complain that yours doesn't match well.
Upvotes: 4