|
|
|
Sometimes application requires multi-language input functionality so user can choose his own language. You can change keyboard layout by Adding and selecting new layout in Control Panel -> Regional Options but how to do the same thing programatically. To change keyboard layout for a specific language you can use a set of APIs.
- GetKeyboardLayout : Returns curent locale identifier (formerly called the keyboard layout) for a specified threadid (zero for current thread).
- GetKeyboardLayoutName : Returns keyboard layout name for a specified keyboard layout ID.
- LoadKeyboardLayout : Loads a new input locale identifier (formerly called the keyboard layout) into the system.
- UnloadkeyboardLayout : Unloads an input locale identifier (formerly called a keyboard layout).
Loading Keyboard Layout
To change the keyboard layout first of all you have to call LoadKeyboardLayout API which takes input local identifier as a parameter. This name is a string composed of the hexadecimal value of the Language Identifier (low word) and a device identifier (high word). For example, U.S. English has a language identifier of 0x0409, so the primary U.S. English layout is named "00000409". Variants of U.S. English layout (such as the Dvorak layout) are named "00010409", "00020409", and so on. Once you have loaded keyboard layout you can activate it using ActivateKeyboardLayout API. Now lets understand what is Language Identifiers.
Language Identifiers
A language identifier is a standard international numeric abbreviation for the language in a country or geographical region. Each language has a unique language identifier (LANGID), a 16-bit value that consists of a primary language identifier and a sublanguage identifier.
+-------------------------+-------------------------+ | SubLanguage ID | Primary Language ID | +-------------------------+-------------------------+ 15 10 9 0 bit
For a list of language identifiers, see Table of Language Identifiers.
Step-By-Step Example
- Create a standard exe project, form1 is added by default - Add three textbox on form1 - Place the following code in form1 |
Click here to copy the following block | Private Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutA" ( _ ByVal pwszKLID As String, ByVal Flags As Long) As Long
Private Declare Function UnloadKeyboardLayout Lib "user32" ( _ ByVal HKL As Long) As Long
Private Declare Function ActivateKeyboardLayout Lib "user32" ( _ ByVal HKL As Long, ByVal Flags As Long) As Long
Private Declare Function GetKeyboardLayout Lib "user32" ( _ ByVal dwLayout As Long) As Long
Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" ( _ ByVal pwszKLID As String) As Long
Const KbdFr = "0000040c" Const KbdEn = "00000409" Const KbdGe = "00000407" Const KbdCh = "00000404"
Private Const KL_NAMELENGTH As Long = 9
Dim OriginalLayoutId As Long Dim OriginalLayoutName As String
Dim hFrLayoutId As Long, hGeLayoutId As Long, hEnLayoutId As Long
Private Sub Form_Load() OriginalLayoutId = GetKeyboardLayout(0)
OriginalLayoutName = String(KL_NAMELENGTH - 1, 0)
GetKeyboardLayoutName OriginalLayoutName
MsgBox "Current keyboard layout is => " & OriginalLayoutName End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) If hEnLayoutId <> 0 Then UnloadKeyboardLayout hEnLayoutId If hEnLayoutId <> 0 Then UnloadKeyboardLayout hEnLayoutId
SwitchToEn End Sub
Private Sub Text3_GotFocus() SwitchToFr End Sub
Private Sub Text2_GotFocus() SwitchToGe End Sub
Private Sub Text1_GotFocus() SwitchToEn End Sub
Sub SwitchToGe() Dim hLayout As Long
hGeLayoutId = LoadKeyboardLayout(KbdGe, 0) ActivateKeyboardLayout hGeLayoutId, 0 End Sub
Sub SwitchToFr() Dim hLayout As Long
hFrLayoutId = LoadKeyboardLayout(KbdFr, 0) ActivateKeyboardLayout hFrLayoutId, 0 End Sub
Sub SwitchToEn() Dim hLayout As Long
hEnLayoutId = LoadKeyboardLayout(KbdEn, 0) ActivateKeyboardLayout hEnLayoutId, 0 End Sub |
- Press F5 to run the project - Now when you change focus to different textbox Keyboard layout is changed. - On form unload english layout is restored. |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|