Contents
Introduction
I think that for most of the non English-native users, Windows OS is installed with at least 2 languages. Changing the language manually is done using ALT+CTRL, but what if you want to write a Win-Form that will select the language automatically? How to select keyboard layout? Keep reading to find out.
Top 
Working with windows API
Please read C# And API's article num.79 [^].
In this article I will demonstrate how use:
Other resources
|
Identifier |
Language |
|
0x0000 |
Language Neutral |
|
0x0409 |
English (United States) |
|
0x040d |
Hebrew |
Top 
Sample code
OK, now that we’ve got the knowledge, let’s start working.
In this demo code I will show how to create a LanguageSelector DLL.
- Create a new C# Class Library project, name it LanguageSelector.
- add :
using System.Runtime.InteropServices;
- add the following constants:
const uint KLF_ACTIVATE = 1; //activate the layout
const int KL_NAMELENGTH = 9; // length of the keyboard buffer
const string LANG_EN_US = "00000409";
const string LANG_HE_IL = "0000040d";
- Next import the LoadKeyboardLayout function from the “user32.dll”
[DllImport("user32.dll")]
private static extern long LoadKeyboardLayout(
string pwszKLID, // input locale identifier
uint Flags // input locale identifier options
);
- The same import for the GetKeyboardLayoutName:
[DllImport("user32.dll")]
//[out] string that receives the name of the locale identifier
private static extern long GetKeyboardLayoutName(
System.Text.StringBuilder pwszKLID
);
- Please notice the StringBuilder – one could use [MarshelAs…] property instead of the StringBuilder. For the simplicity of the code and not dealing with Managed / Unmanaged code, I didn’t use the marshaling system.
Top 
Adding functionality
Top 
Retrieve current keyboard layout name
The code below warps the GetKeyboardLayoutName DLL function into more convenient method, easy to use later on.
public static string getName()
{
System.Text.StringBuilder name = new System.Text.StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(name);
return name.ToString();
}
Tip: One should use the above function in order to discover the installed languages on the target machine.
Top 
Changing the keyboard layout
The next two functions force the keyboard layout to be changed. On my machine the Hebrew and English layouts are installed, one may add additional languages upon demand, using the appropriate language identifier.
public static void Hebrew()
{
//load and activate the layout for the current thread
LoadKeyboardLayout(LANG_HE_IL, KLF_ACTIVATE);
}
public static void English()
{
//load and activate the layout for the current thread
LoadKeyboardLayout(LANG_EN_US, KLF_ACTIVATE);
}
Top 
Building a winForm driver
Add a new C# windows application and place 3 buttons as shown below:

Add reference to the LanguageSelector Dll project, also type: using LanguageSelector;
OK, now we are ready to write the click code for each button:
private void buttonHE_Click(object sender, System.EventArgs e)
{
KeyboardLayout.Hebrew();
}
private void buttonEN_Click(object sender, System.EventArgs e)
{
KeyboardLayout.English();
}
private void buttonGetID_Click(object sender, System.EventArgs e){
string keyboardID;
keyboardID = KeyboardLayout.getName();
MessageBox.Show(this,"Current keyboard layout ID: " + keyboardID,
"Keyboard layout",MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
By clicking the Get Language ID button a message box such that will be shown.

Top 