KMKB0093: HOWTO: Switch keyboard and font together in Word 64-bit

In Keyman 9.0, the Keyman COM API is not available for 64-bit editions. Therefore, you cannot use Keyman's COM API to switch keyboards (see KMKB0033 for an example for 32-bit editions of Word). This KB shows an example of how to switch keyboards using Windows API calls.

To create these macros, you will need to do the following:

  1. In Word, press Alt+F11.
  2. In the Project pane, double-click Normal->Microsoft Word Objects->ThisDocument
  3. Paste the functions in and change the fonts and keyboard IDs as desired. You can add additional functions for the extra keyboards you want to activate. The ShowLoadedLayouts() function lists the IDs for all loaded keyboards, which you can then place into the other functions as you desire.

You could assign hotkeys or toolbar buttons to these routines. See the following links for more details:

The macros are presented below:

'
' Keyboard switching in Visual Basic for Applications x64
' -------------------------------------------------------
'
Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As LongPtr, _
    ByVal flags As Long) As Long
Private Declare PtrSafe Function GetKeyboardLayoutList Lib "user32" (ByVal size As Long, _
    ByRef layouts As LongPtr) As Long

Sub SwitchKhmerOn()
    Selection.Font.Name = "Khmer OS Content"
    ActivateKeyboardLayout &H4530453, 0
End Sub

Sub SwitchKeymanOff()
    Selection.Font.Name = "Arial"
    ActivateKeyboardLayout &H4090C09, 0
End Sub

Sub ShowLoadedLayouts()
    Dim numLayouts As Long
    Dim i As Long
    Dim layouts() As LongPtr

    numLayouts = GetKeyboardLayoutList(0, ByVal 0&)
    ReDim layouts(numLayouts - 1)
    GetKeyboardLayoutList numLayouts, layouts(0)

    Dim msg As String
    msg = "Loaded keyboard layouts: " & vbCrLf & vbCrLf

    For i = 0 To numLayouts - 1
        msg = msg & Hex(layouts(i)) & vbCrLf
    Next

    MsgBox msg
End Sub

This method has some limitations. For example, you cannot switch between multiple keyboard layouts installed for a single language.


Applies to:

  • Keyman Desktop 8.0
  • Keyman Desktop 9.0

KB article KMKB0093 created on 03 Dec 2016

Browse all knowledge base articles