'This example was created by Kirill V. Rodionov
'This program needs a Command Button, named Command1

Private Const KL_NAMELENGTH As Long = 9
Private Const HKL_NEXT As Long = 1
Private Const HKL_PREV As Long = 0

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

Private Sub Command1_Click()
Dim lngCurrent_HKL As Long
Dim strCurrent_Buffer As String
Dim lngSwitched_HKL As Long
Dim strSwitched_Buffer As String

'Retrieves current handle to the keyboard layout
   lngCurrent_HKL = GetKeyboardLayout(0)
  
'creates a buffers
   strCurrent_Buffer = String(KL_NAMELENGTH - 1, 0)
   strSwitched_Buffer = String(KL_NAMELENGTH - 1, 0)
  
'Retrieves current name of the active keyboard layout
   GetKeyboardLayoutName strCurrent_Buffer
  
'Switches to the next keyboard layout
   ActivateKeyboardLayout HKL_NEXT, 0
  
'Retrieves "switched" handle to the keyboard layout
   lngSwitched_HKL = GetKeyboardLayout(0)
  
'Retrieves "switched" name of the active keyboard layout
   GetKeyboardLayoutName strSwitched_Buffer
   MsgBox "Keyboard layout was switched" & vbCrLf & "from " & strCurrent_Buffer _
      & " to " & strSwitched_Buffer
     
'Returns to the initial keyboard layout
   ActivateKeyboardLayout lngCurrentHKL, 0
End Sub

Close this window