KeyBoardLayout and Language Selector
<!–[if supportFields]><![endif]–>Introduction
I think that formost of the non English-native users, Windows OS is installed with at least 2languages. Changing the language manually is done using ALT+CTRL, but what ifyou want to write a Win-Form that will select the language automatically? Howto select keyboard layout? Keep reading to find out.
Workingwith windows API
Please read C# And API's articlenum.79.
In this article Iwill demonstrate how use:
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
Other resources<o:p></o:p>
- Languages identifier: (complete list is here Languages table)
|
Identifier<o:p></o:p> |
Language<o:p></o:p> |
|
0×0000<o:p></o:p> |
Language Neutral<o:p></o:p> |
|
0×0409<o:p></o:p> |
English (United States)<o:p></o:p> |
|
0x040d<o:p></o:p> |
Hebrew<o:p></o:p> |
Samplecode
OK, now that we?vegot the knowledge, let?s start working.
In this demo codeI 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<
/span> Flags // input locale identifier options
);<o:p></o:p> - The same import for the GetKeyboardLayoutName:
[DllImport("user32.dll")]
private static extern long GetKeyboardLayoutName(
System.Text.StringBuilder pwszKLID //[out] string that receives the name of the locale identifier
);<o:p></o:p>
- 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.
Adding functionality
Retrievecurrent keyboard layout name
The code below warps the GetKeyboardLayoutName DLL function into more convenient method, easy to use later on.
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
public static stringgetName()<o:p></o:p>
{<o:p></o:p>
System.Text.StringBuildername = newSystem.Text.StringBuilder(KL_NAMELENGTH);<o:p></o:p>
GetKeyboardLayoutName(name);<o:p></o:p>
return name.ToString();<o:p></o:p>
}<o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
Tip: One should use theabove function in order to discover the installed languages on the targetmachine.<o:p></o:p>
Changingthe keyboard layout
The next two functions force the keyboardlayout to be changed. On my machine the Hebrew and English layouts areinstalled, one may add additional languages upon demand, using the appropriatelanguage identifier.
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
public static void Hebrew()<o:p></o:p>
{<o:p></o:p>
//load and activate the layout for the current thread<o:p></o:p>
LoadKeyboardLayout(LANG_HE_IL,KLF_ACTIVATE);<o:p></o:p>
}<o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
public static void English()<o:p></o:p>
{<o:p></o:p>
//load and activate the layout for the current thread<o:p></o:p>
LoadKeyboardLayout(LANG_EN_US,KLF_ACTIVATE);<o:p></o:p>
}<o:p></o:p>
<!–[if !supportEmptyParas]–> <!
–[endif]–><o:p></o:p>
Building a winForm driver
Add a new C# windows application and place3 buttons as shown below:
<!–[if gte vml 1]–><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"> <v:f eqn="sum @0 1 0"> <v:f eqn="sum 0 0 @1"> <v:f eqn="prod @2 1 2"> <v:f eqn="prod @3 21600 pixelWidth"> <v:f eqn="prod @3 21600 pixelHeight"> <v:f eqn="sum @0 0 1"> <v:f eqn="prod @6 1 2"> <v:f eqn="prod @7 21600 pixelWidth"> <v:f eqn="sum @8 21600 0"> <v:f eqn="prod @7 21600 pixelHeight"> <v:f eqn="sum @10 21600 0"> </v:f> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"> <o:lock v:ext="edit" aspectratio="t"></o:lock><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width: 203.25pt; height: 173.25pt;"> <v:imagedata src="http://www.csharphelp.com/archives2/KeyBoardLayout%20and%20Language%20selector_files/image001.png"></v:imagedata>
<!–[endif]–></v:shape></v:path></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas></v:stroke></v:shapetype>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
Add reference to the LanguageSelector Dllproject, also type: using LanguageSelector;<o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
OK, now we are ready to write the clickcode for each button:
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
private void buttonHE_Click(objectsender, System.EventArgs e)<o:p></o:p>
{<o:p></o:p>
KeyboardLayout.Hebrew();<o:p></o:p>
}<o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
private void buttonEN_Click(objectsender, System.EventArgs e)<o:p></o:p>
{<o:p></o:p>
KeyboardLayout.English();<o:p></o:p>
}<o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
private void buttonGetID_Click(objectsender, System.EventArgs e)<o:p></o:p>
{<o:p></o:p>
string keyboardID;<o:p></o:p>
keyboardID= KeyboardLayout.getName();
MessageBox.Show(this,"Currentkeyboard layout ID: " + keyboardID,
"Keyboard layout",MessageBoxButtons.OK,
MessageBoxIcon.Information);<o:p></o:p>
}<o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
Byclicking the Get Language ID button a message box such that will be shown.<o:p></o:p>
<!–[if gte vml 1]–><v:shape id="_x0000_i1026" type="#_x0000_t75" style="width: 197.25pt; height: 89.25pt;"> <v:imagedata src="http://www.csharphelp.com/archives2/KeyBoardLayout%20and%20Language%20selector_files/image003.png"></v:imagedata>
</v:shape>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
<!–[if !supportEmptyParas]–> <!–[endif]–><o:p></o:p>
About the Author <o:p></o:p>
My name is Yariv Amar, currently (2003) I'ma college student studying a Bsc in Computer Science at The Academic College of Tel-Aviv Jafo.<o:p></o:p>
You can find out some more interesting codeat my site http://yarix1.tripod.com












No comments yet... Be the first to leave a reply!