Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
#HotIf (getActiveWindowKeyboardLayout() & 0xFFFFFFFF == 0xF0070415)
F7:: {
msgbox("Polish (214)")
}
#HotIf (getActiveWindowKeyboardLayout() & 0xFFFFFFFF == 0x04150415)
F7:: {
msgbox("Polish (Programmers)")
}
#HotIf
getActiveWindowKeyboardLayout() {
hKL := 0
if (hWnd := winExist("A")) {
idThread := dllCall("User32.dll\GetWindowThreadProcessId", "Ptr",hWnd, "Ptr",0, "UInt")
hKL := dllCall("User32.dll\GetKeyboardLayout", "UInt",idThread, "Ptr")
}
return hKL
}
As you can see from the existing code, only the lower bits of the
hKL are being extracted for evaluation.
I don’t even agree with the approach of extracting
langID by masking with
0x3FFF (I believe treating the entire lower WORD as
langID by using
0xFFFF is more accurate).
However, in this case, relying on
langID alone is insufficient.
Since it is the same language, the difference in the upper WORD, the
deviceID, must be compared.
In that case, it would be better to compare the entire
hKL instead.
Details related to language layouts like this are not documented.
The code I provided is not entirely precise, but it should be able to distinguish keyboard layouts in most environments.
Lastly, the reason for masking
hKL with
0xFFFFFFFF for comparison is due to differences in the return values between 32-bit and 64-bit environments.
In this case, the Polish (214) keyboard would display different return values for each.
+)
Code: Select all
#define MAKELANGID(p, s) ((((WORD )(s)) << 10) | (WORD )(p))
Code: Select all
msgbox(format("0x{:X}", 0x3F << 10 | 0x03FF)) ; 0xFFFF
MAKELANGID macro (winnt.h)
I was personally curious, so I just calculated it, and it seems that the maximum value a
langID can have is
0xFFFF.