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

// Keyboard layout structure
struct _KeyboardLayout
{
	HKL hArabic;
	bool bArabic;
	HKL hEnglish;
	bool bEnglish;
	HKL hRussian;
	bool bRussian;
	HKL hUzbek;
	bool bUzbek;
} KeyboardLayout;

// setting default values (as not installed = false)
KeyboardLayout.bArabic=false;
KeyboardLayout.bEnglish=false;
KeyboardLayout.bRussian=false;
KeyboardLayout.bUzbek=false;


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

// Get layout’s count 
int nBuff;
TCHAR szKeyboard[KL_NAMELENGTH];
nBuff=::GetKeyboardLayoutList(0, NULL);
HKL * phkl;
if ((phkl = new HKL[nBuff])==NULL)
{
	AfxMessageBox(_T("Out of Memory !"));
	return;
}

// Get and parse layout items 
::GetKeyboardLayoutList(nBuff,phkl);
// iterate through list to check needed layouts 
for(int nKeyboard=0; nKeyboard<nBuff; nKeyboard++)
{
	::ActivateKeyboardLayout(phkl[nKeyboard], KLF_SETFORPROCESS);
	GetKeyboardLayoutName(szKeyboard);
	if(wcscmp(szKeyboard,_T("00000401"))==0)
	{
		KeyboardLayout.bArabic=true;
		KeyboardLayout.hArabic=phkl[nKeyboard];
	}
	if(wcscmp(szKeyboard,_T("00000409"))==0)
	{
		KeyboardLayout.bEnglish=true;
		KeyboardLayout.hEnglish=phkl[nKeyboard];
	}
	if(wcscmp(szKeyboard,_T("00000419"))==0)
	{
		KeyboardLayout.bRussian=true;
		KeyboardLayout.hRussian=phkl[nKeyboard];
	}
	if(wcscmp(szKeyboard,_T("00000843"))==0)
	{
		KeyboardLayout.bUzbek=true;
		KeyboardLayout.hUzbek=phkl[nKeyboard];
	}
}
delete [] phkl;


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

if(!KeyboardLayout.bArabic)
	MessageBox(NULL,_T("Arabic (Saudi Arabic) keyboard layout not installed"),_T("Warning"),MB_OK|MB_ICONEXCLAMATION);
if(!KeyboardLayout.bEnglish)
	MessageBox(NULL,_T("English (USA) keyboard layout not installed"),_T("Warning"),MB_OK|MB_ICONEXCLAMATION);
if(!KeyboardLayout.bRussian)
	MessageBox(NULL,_T("Russian keyboard layout not installed"),_T("Warning"),MB_OK|MB_ICONEXCLAMATION);
if(!KeyboardLayout.bUzbek)
	MessageBox(NULL,_T("Uzbek (Cyrillic) keyboard layout not installed"),_T("Warning"),MB_OK|MB_ICONEXCLAMATION);


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

if(KeyboardLayout.bArabic)
   ::ActivateKeyboardLayout(KeyboardLayout.hArabic, KLF_SETFORPROCESS);


IT / Coding / How-To


Other Topics