Invoking Keymanapi from VBA

Hello to all,

After much delay I am now using a lull of activity during the holiday period to try and convert a MS-Access VBA application that programmatically invokes Keyman (V9) though the old kmcomapi library to a system using the latest version of keyman (V13) using keymanapi.

To make a long story short - this is a multilingual database and the keyboard needs to be automatically changed in some fields based on the value of other fields. So if field A is “Russian” for example then field B should shift to a cyrillic keyboard…if field A is “Hindi” then field B should shift to a Devanagari keyboard…etc…

In any case I have started to look at the API documentation at a link Mark gave me a while back: Keyman Engine for Windows 10.0 API

But (lack of programming experience I suppose) I’m struggling on how to use this from VBA.
Are there any examples I could get my hands on, something along the lines of Keyman Support | HOWTO: Switch keyboard and font together in Word 64-bit ### **NOTE**: This archived documentation has not been updated recently and may contain information that is no longer relevant <p>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 <a href=''>KMKB0033</a> for an example for 32-bit editions of Word). This KB shows an example of how to switch keyboards using Windows API calls.</p> <p>To create these macros, you will need to do the following:</p> <ol> <li>In Word, press Alt+F11.</li> <li>In the Project pane, double-click Normal->Microsoft Word Objects->ThisDocument <li>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. </ol> <p>You could assign hotkeys or toolbar buttons to these routines. See the following links for more details:</p> <ul> <li><a href=''>Assign a macro to a toolbar</a></li> <li><a href=''>Assign a macro to a hotkey</a></li> <li><a href=''>Assign a macro to the Ribbon in Word 2007</a></li> </ul> <p>The macros are presented below:</p> <blockquote style='background: #c0c0c0; padding: 8px; border: solid 1px #808080'><pre>' ' 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</pre></blockquote> <p>This method has some limitations. For example, you cannot switch between multiple keyboard layouts installed for a single language.</p> ## Applies to: * Keyman Desktop 8.0 * Keyman Desktop 9.0 for V9/kmcomapi ?

Any hints / links would be greatly appreciated ! :slight_smile:


Please hold on as our developers are on leave till early next year. Thanks.

Hi Makara,
Of course, no rush :slight_smile:

I’ve just returned from holiday and am now working through my backlog :slight_smile:

The best way to do this now doesn’t require the Keyman API but rather activates keyboards based on the Windows language using the Windows ActivateKeyboardLayout API. The Keyman keyboard will be activated when you use the Windows API. For VBA, you would use the code from KB0093 that you referenced.

You can use the ShowLoadedLayouts() function to list the identifier for each layout that you have loaded on your system, and then copy the SwitchKhmerOn() function, using the appropriate identifier for the keyboard you wish to activate.

See also Note that this is the same as you referenced in KB0093 but the additional background at that link may be helpful.

@Den_Hol Does the reply from Marc help?

Hi Makara,
I think it does – I’ll try to implement and come back if I hit any issues.


1 Like