Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


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"  'French (standard)'//by default installed
Const KbdEn = "00000409"  'English(US)   '//by default installed
Const KbdGe = "00000407"  'German (standard)'//by default installed
Const KbdCh = "00000404"  'Chinese(Taiwan) '//not default - wont work if not installed

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()
  'Retrieves current handle to the keyboard layout
  OriginalLayoutId = GetKeyboardLayout(0)

  'creates a buffers
  OriginalLayoutName = String(KL_NAMELENGTH - 1, 0)

  'Retrieves current name of the active keyboard layout
  GetKeyboardLayoutName OriginalLayoutName

  MsgBox "Current keyboard layout is => " & OriginalLayoutName
End Sub

'//Make sure that we unload all other layout and switch to
'//the default layout when exit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  '//Unload Franch and German layout which we loaded for demo purpose
  If hEnLayoutId <> 0 Then UnloadKeyboardLayout hEnLayoutId
  If hEnLayoutId <> 0 Then UnloadKeyboardLayout hEnLayoutId

  '//I assume that English is your primary layout so activate it before unload
  SwitchToEn
End Sub

'//Text2 takes French input
Private Sub Text3_GotFocus()
  SwitchToFr
End Sub

'//Text2 takes German input
Private Sub Text2_GotFocus()
  SwitchToGe
End Sub

'//Text1 takes English input
Private Sub Text1_GotFocus()
  SwitchToEn
End Sub

Sub SwitchToGe()
  Dim hLayout As Long

  '//Loading Keyboard requires string identifier
  hGeLayoutId = LoadKeyboardLayout(KbdGe, 0)
  ActivateKeyboardLayout hGeLayoutId, 0
End Sub

Sub SwitchToFr()
  Dim hLayout As Long

  '//Loading Keyboard requires string identifier
  hFrLayoutId = LoadKeyboardLayout(KbdFr, 0)
  ActivateKeyboardLayout hFrLayoutId, 0
End Sub

Sub SwitchToEn()
  Dim hLayout As Long

  '//Loading Keyboard requires string identifier
  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 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.