how to programmatically install a keyboard layout?

Alan Sinclair 11 Reputation points
2020-10-21T19:49:20.803+00:00

I need to package several different keyboard layouts aka "input locale identifiers" together, so I need to write some code to install a layout in Windows, but haven't found any documentation. Can anyone show me example code or point me to documentation? Backgound: Microsoft's MSKLC tool enables creation of Keyboard Layouts. Each single layout is 'contained' in a DLL; for a layout MSKLC creates four DLLs for various flavors of Windows (naming them i386, amd64, ia64, wow64). MSKLC also creates the three appropriate MSI installers for that single layout, but those installers cannot be used in this situation. Just deploying the DLLs doesn't enable them, there is obviously some action needed to add the new locale identifiers to what Windows knows about its keyboards.

(I'm unsure this is the right place for this question, and what tags should be used .. please correct me if necessary)

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,412 questions
{count} vote

17 answers

Sort by: Most helpful
  1. Drake Wu - MSFT 991 Reputation points
    2020-10-29T06:28:48.37+00:00

    Hi @Alan Sinclair Please check if this Keyboard Layout Samples and the steps in it are what you need.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Alan Sinclair 11 Reputation points
    2020-12-09T02:32:12.08+00:00

    Returning to ask yet again for help from Microsoft on this problem. I've installed a keyboard layout but cannot activate it programatically (however it will activate from the Language Bar, so it's probably correctly installed.) Here's sample code, where the default layout is US English and the newly-installed layout is British English aka UK English. This code isn't installing the layout, just trying to activate it):

    #include "Windows.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    void displayError() {
    
        DWORD dwError = GetLastError();
        printf("Failed with code %d\n", dwError);
        exit(dwError);
    }
    void showKbdName() {
        wchar_t szKb[500];
        BOOL bStatus = GetKeyboardLayoutName(szKb);
        if (bStatus == 0) {
            displayError();
        }
        printf("Lang is %ls\n", szKb);
    }
    
    
    int main()
    {
        cout << "Hello World!\n";
        showKbdName();
    
        //LPCWSTR idEnUS = L"00000409";  // US 
        LPCWSTR idEnUK = L"A0000809";  // UK 
        //HKL oldHkl = ActivateKeyboardLayout(LoadKeyboardLayout(idEnUK, KLF_ACTIVATE), KLF_REORDER);
        HKL hklNewLayout = LoadKeyboardLayout(idEnUK, KLF_ACTIVATE);   
        HKL oldHkl = ActivateKeyboardLayout(hklNewLayout, KLF_REORDER);
        if (oldHkl == 0) {
            displayError();
        }
    
        showKbdName();
        // shows Kbd as A0000809  i.e. UK English
        // but Language Bar still shows US English
    
        string strTmp;
        getline(cin, strTmp);       // UK English kbd is NOT in use here
        cout << "\ninput was '" << strTmp << "'\n\n";
    }
    

    As in the code comment, the showKbdName function shows the layout as UK English after the Activate call, but actual keyboard input is still using the US layout (e.g. shift-3 get the US '#' not the UK '£') and the Language Bar still shows US.

    HOW can keyboard layout be programatically switched?

    1 person found this answer helpful.
    0 comments No comments

  3. Alan Sinclair 11 Reputation points
    2020-10-28T01:04:54.293+00:00

    > What kind of project are you using?

    WiX Toolset and Visual Studio 2017 writing C++ code.

    Using the WiX Toolset I'm making an MSI install package to deploy an input locale definition file; for reference I'll call that file LOCALE.DLL

    LOCALE.DLL is created by MSKLC to define the new keyboard layout. The specific language will already be installed on the target system; this new input locale remaps various keys for our specific needs. Msiexec.exe (the native Windows Installer) will install LOCALE.DLL from my MSI file.

    My MSI needs a custom action written in C++ using Visual Studio 2017

    As msiexec deploys LOCALE.DLL from my MSI, msiexec will call functions in a custom action binary (for reference I'll call this binary CUSTOM.DLL) which in turn calls WINAPI functions to "activate" the input locale: e.g. to add the keyboard configuration defined in LOCALE.DLL to the Windows Language Bar, etc. (CUSTOM.DLL is not deployed on the target system; it remains embedded in the MSI)

    For what it's worth, I'm familiar with writing custom action DLLs for Windows Installer; what I'm missing is how to activate the input locale provided in LOCALE.DLL which my MSI deploys.

    I think CUSTOM.DLL will need to use LoadKeyboardLayout and ActivateKeyboardLayout WINAPI functions but I have not found documentation

    Any pointers to documentation or example code you can give will be much appreciated!

    Thanks


  4. Alan Sinclair 11 Reputation points
    2020-10-28T19:45:56.92+00:00

    Sorry, I confused the issue. This is NOT about the Wix Toolset.

    I am asking what C++ code do I write to install an input locale (aka keyboard layout).

    Please can you give me a pointer to C++ or WINAPI documentation or example code for how to install an input locale?

    (The wix tool is just a deployment mechanism and not really relevant to my question. I mentioned it to give the full background, but it's unnecessary. I can use a C++ exe if you tell me where to find example or documentation.)

    thanks

    0 comments No comments

  5. Alan Sinclair 11 Reputation points
    2020-11-09T20:26:40.827+00:00

    @Drake Wu - MSFT
    Thanks DrakeWu .. the link gives samples of how to create new Keyboard Layouts, but what I need is example of how to USE a new layout -- i.e. how to install the new layout in Windows.

    Is there any example code to use LoadKeyboardLayout API, and/or ActivateKeyboardLayout API ?

    thanks
    Alan