Search code examples
c#.netkeyboardpinvokeculture

Change Windows keyboard layout using c#


Is it possible to change the Windows OS keyboard layout (from azerty to querty for example) using C# ? (for the entire OS, not just the process of the current application)

Do i have to use PInvoke or is there a simpler api/way to do so?


Solution

  • It's a single call:

    [DllImport("user32.dll")]
    static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);
    

    Sample:

    For Adding and Activating U.S. English Dvorak layout, use "00010409".

    LoadKeyboardLayout("00010409", 1)
    

    MSDN:

    Beginning in Windows 8: The input locale identifier is loaded for the entire system. This function has no effect if the current process does not own the window with keyboard focus.

    Meaning: Your window must be in focus otherwise the function has no effect.

    Full documentation from MSDN: