Multi language on-screen keyboard using Windows keyboard dlls – Lars Werner

Multi language on-screen keyboard using Windows keyboard dlls


Multi language - Hello World preview


The multi language onscreen keyboard is a result of "lazyness" to not invent the wheel again.It reads all the keyboard layouts that are available in the windows-directory and layout is presented with a Apple / Android / Windows 8 look-a-like keyboard. Deadkeys are yellow and presented on a seperate row, all available keys are shown.

The keyboard has been tested on Windows XP / 2003 server / Vista / 7 / 2008 server / 8, all operating systems seem to work as expected.


My inital start was to create own keyboard maps manually. Going through each language, using the Microsoft On-screen Keyboard. After I started on my third keyboard map, I got fed up and started searching for solutions to read the existing layouts.

In my quest I discovered a bug while loading keyboards on x86/x64 systems, the solution can be found here. The same technique is used in this application. The solution is two different kbd.h versions, one for x86 and one for x64. I wanted to know what caused this problem, so I contacted the Microsoft-keyboard-guru Michael Kaplan but his answer was negative, since this is "undocumented". His thumb down made me even more persistent to actually complete this project, here we are 🙂
Another smart guy "kwhat" made another solution that shows the x86/x64 bit-shifts more clear, his code is also usable with GNU Compiler Collection (GCC).

(Note: Please share if you know any good APIs for getting the keyboard layout. I’m looking for approache/API/function that extract the scan code + character relationship; comment your solution below!)

Scan codes

Scan codes are the actually physical reference for a key on the keyboard, a hex-number.
See the picture to get an overview, an English layout uses the 0x10 as "q"-key, the 0x11 is "w", the 0x1E is "a", and so on…

The main work done by this class is to sort out all the characters and link them to correct scan code.
There are three different scan codes registers as described in wiki, Microsoft uses set 1; IBM PC XT.

Using the code

There are a split between the loading of a keyboard layout and the presentation of it.

Loading keyboard dlls

There are one class that handles the loading of DLL layouts; CKLL (Keyboard Layout Loader).
The function LoadDLL loads the keyboard layout, and afterwards the function GetCharFromSC is used to retrive a VK_CHAR based on scancode and keyboard-state. The variables returned are:

The keyboard states are the variation of common-modifiers:
Each keyboard layout dll has a file description that make a reasonable description of the layout. This info can be retrived by calling CKeyboardStatic. (Only in MFC-version)


The presentation is done by a CKeyButton derivate called OSKScanCodes.
It has a subclass called CKeyboardStatic that contain each character with the scan code, character and state.
Which key goes where are linked to the array CKLL, in this version the standard qwerty is used. A change of this table will affect presentation directly.


The different presentations was made just for fun 🙂 The picture below shows the variations of presentations, function of keyboard is equal in all three:

By using GDI+ it is possible to create good looking graphics, using AA and the ARGB-coloring, that is the technique used here. The Apple style require much painting to replicate, but it sure looks the best of all three (in my eyes…)

OSKScanCodes has these public functions, read comment behind for description.
SetKeyboardPresentation()Using the OSK_KEYBOARD_STATES class you can set the scan code + character, using the SetKeyboardLayout() to link to the "physical" key, based on the CKLL-array. The CKLL changes the characters, based on the CPtrArray-array value. The CKeyboardStatic changes the presentation of keyboard.

MFC or not to MFC

The CKLL class comes in two flavors, one MFC version and one MFC-free.

The MFC-free wstring uses a vector based array to to keep track of scan codes and characters instead of CKLL.
The is a pure MFC and is not straightforward to convert to other types.

Project "ConsoleKeyboard" is a pure win32 console application and the string handling between has changed to (for this mod.) It was made to show off how little code needed to present an keyboard. See picture below for results:

You have to change the console font to a unicode supported one, select Lucida Console or Consolas.
Rightclick on header and select "Properties" to change it.

The source for getting this presentation is simple (code has been stripped for errorhandling for easy reading):
By using class you could easly make an multilanguage onscreen keyboard for a OpenGL, QT, or any other implementation in Windows.


Most of the layouts has not been confirm, and frankly I don’t "understand" all of them. Please take time to check your day-by-day keyboard layout and see if the keys are placed in your normal position.
The keyboard does not present modifiers and functions keys as shift, control, enter, space, tab etc… I did not use any time to include this so concider the presentation as a proof-of-concept.
In some layouts a square is presented as char, that is normally not a unicode-character, that isn’t handled. It will might be fixed, in a future version.
Microsoft keyboard DLLs can have multiple layouts in them, this class does only handle the first item we find.


v1.00 – First public release

 Posted by at 00:30:02

Leave a Reply

%d bloggers like this: