Several times, a question came up how to synthesize keyboard input to a remote system given a text string. The remote system is typically but not necessarily a VM. That sounds like something which should be trivial, yet it is anything but.
The basic problem is that keyboards send scan codes which reflect the position of the key on the keyboard, not the character which a key press generates. The compounding problem is that there is a nearly infinite number of key to character mappings, also known as keyboard layouts.
And even for the most basic alphanumeric input, the keyboard layouts are crucial, because entering something as simple as ‘abc123’ requires a different sequence of keystrokes on US, German, and French keyboards.
If the sending system runs a Windows OS, the problem scope can be greatly reduced through several APIs: OemKeyScan, VkKeyScan, VkKeyScanEx, MapVirtualKey, and MapVirtualKeyEx. These all translate characters or virtual key codes into scan codes, either using the current keyboard layout or given a specific keyboard layout. Sounds great, right? Not so fast… Continue reading







