Changing keyboard layout programmatically (MS Visual C++ / WinAPI)

Long years ago I was tasked with a large database project (MS Visual C++, MS SQL Server), related to multi-language data entry, data processing and reporting tasks. As a key developer I worked in close touch with customer’s staff to monitor whole process; operatively fixing problems and optimizing overall functionality of developed application. The data entry process required filling several forms simultaneously in different languages, and, I noticed that company operators spending their time on switching between keyboard layouts and scrolling/navigating forms.

As an experiment I advised to company some modifications to the developed application to change keyboard layouts automatically to desired language, based on form field's language. For example, when operator fills text in English field then keyboard layout would switch automatically to English, when operator fills Russian or Arabic text field keyboard layout changes automatically according to the form field’s desired language. As I remember, together with automatic form scrolling effect, this simple trick helped to dramatically increase the performance of operator’s data entry job .

The working algorithm:

  1. Define a structure with desired layout handles
  2. Get installed system keyboard layouts with WinAPI function call
  3. Warn user if required layout is not installed on system
  4. Switch keyboard layout on focus method of text field

I think source code of this solution is self explanatory (heavily uses WinAPI functions)

1. Define a structure with desired layout handles

2. Getting list of installed system keyboard layouts through WinAPI function call

3. After loading layout handles, check if all required layouts are installed in system

4. To activate specific layout I've used functions like this(on text field’s focus methods):