0

I'm trying to conditionally set a keyboard layout on all opened windows in the following way:

  1. Calling getKeyboardLayout, and determining what's the current language (English or Russian).
  2. Calling setDefaultKeyboard (using a script I've found here), passing the other language.

Here's the full script:

EN := 0x0409
RUS := 0x0419

currentKeyboardLayout := getCurrentKeyboardLayout()
setDefaultKeyboard(currentKeyboardLayout = EN ? RUS : EN)

getCurrentKeyboardLayout() {
  SetFormat, Integer, H
  WinGet, WinID,, A
  ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
  priorHKL :=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
  Return priorHKL
}
setDefaultKeyboard(LocaleID){
    Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2

    Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
    VarSetCapacity(binaryLocaleID, 4, 0)
    NumPut(LocaleID, binaryLocaleID)
    DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", &binaryLocaleID, "UInt", SPIF_SENDWININICHANGE)

    WinGet, windows, List
    Loop % windows {
        PostMessage 0x50, 0, % Lan, , % "ahk_id " windows%A_Index%
    }
}

This script doesn't work in its current form. Specifically, it seems that the call for getCurrentKeyboardLayout causes PostMessage to not work. Note that if instead of:

PostMessage 0x50, 0, % Lan, , % "ahk_id " windows%A_Index%

we'd use:

PostMessage 0x50, 0, % Lan, , A

then it would "work", but won't affect all windows, which is not what I need.

So my question is:
Why does calling getCurrentKeyboardLayout causes PostMessage to not work, and how can I overcome it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.