Title: Activate Keyboard Layout / Input Identifier Using Delphi
If you need to allow the user of your Delphi application to use different languages / keyboard layouts to input data, you would need to mimic the "Text Service and Input Languages" Control Panel applet.
Using Control Panel you can specify the default language that you use to insert text. The default language is used every time you start or log on to your computer. For example, if you insert text in German most of the time, but have also added English as an input language, select German as your default language. When you want to insert text in English, you can switch to that language.
Programmatically Switch Between Languages
If a user has German and English loaded, but you want the user to be able to enter data in Polish, you need to load and activate the Polish language / keyboard layout.
The LoadKeyboardLayout API function, defined in the Windows.pas unit, loads a new input locale identifier (formerly called the keyboard layout) into the system.
LoadKeyboardLayout takes two parameters: the name of the input locale identifier to load and how the input locale identifier is to be loaded.
The first parameter (locale identifier name) is a hex string consists of a primary language identifier and a sublanguage identifier.
Sample Language Switcher
Drop a TRadioGroup control (named "RadioGroup1") and a TButton (named "Button1") on a form.
Add items to RadioGroup1: 'LANG_CROATIAN', 'LANG_FRENCH', 'LANG_GERMAN', 'LANG_ENGLISH', 'LANG_ITALIAN'.
Note that "LANG_???" constants are defined in the Windows.pas unit.
//the RadioGroup1 ItemIndex corresponds to the index of the "LANG" identfier in the KLS array.
procedure TLanguageForm.Button1Click(Sender: TObject);
const
KLS : array[0..4] of HKL = (LANG_CROATIAN, LANG_FRENCH, LANG_GERMAN, LANG_ENGLISH, LANG_ITALIAN);
procedure SetKeyboardLayout(const klsIndex: integer);
var
klId : array [0..9] of char;
keyboardCode : HKL;
begin
keyboardCode := (SUBLANG_DEFAULT shl 10) or KLS[klsIndex];
StrPCopy(klId, IntToHex(keyboardCode, 8));
LoadKeyboardLayout(klId, KLF_ACTIVATE);
end; (*SetKeyboardLayout*)
begin
SetKeyboardLayout(RadioGroup1.ItemIndex);
end;
Note: the above code uses default sublanguage identifier to convert the LANG_??? value to the hexadecimal value of the language identifier.
For more info on programmatically working with keyboard layouts (input locale identifiers) visit Microsoft MSDN.