如何在AutoHotkey中获得当前的键盘布局(语言设置)



我想有可能让我的脚本是敏感的当前键盘布局。就像

#If %keyboardLang% == en-US
a::
MsgBox, I pressed a on an english keyboard
return
#If %keyboardLang% == de-US
a::
MsgBox, I pressed a on an german keyboard
return

@Cadoiz为你指明了正确的方向。根据MSDN,返回的值实际上是2:

要加载的输入区域设置标识符的名称。该名称是由语言标识符(低字)和设备标识符(高字)的十六进制值组成的字符串。例如,美式英语的语言标识符为0x0409,因此主要的美式英语布局命名为"00000409"。美式英语布局的变体(如Dvorak布局)被命名为"00010409", "00020409",等等。

所以可以有很多组合,你需要的是第二个值。所以我想到了这个:

F1::
MsgBox % "Keyboard layout: " GetLayout(Language := "") "`n"
. "Keyboard language identifier: " Language
return
#if GetLayout(Language := "") = "gr"
a::MsgBox 0x40, Layout, % "Current layout: " Language
#if GetLayout(Language := "") = "us"
a::MsgBox 0x40, Layout, % "Current layout: " Language
#if
GetLayout(ByRef Language := "")
{
hWnd := WinExist("A")
ThreadID := DllCall("GetWindowThreadProcessId", "Ptr",hWnd, "Ptr",0)
KLID := DllCall("GetKeyboardLayout", "Ptr",ThreadID, "Ptr")
KLID := Format("0x{:x}", KLID)
Lang := "0x" A_Language
Locale := KLID & Lang
Info := KeyboardInfo()
return Info.Layout[Locale], Language := Info.Language[Locale]
}
KeyboardInfo()
{
static Out := {}
if Out.Count()
return Out
Layout := {}
loop reg, HKLMSYSTEMCurrentControlSetControlKeyboard LayoutDosKeybCodes
{
RegRead Data
Code := "0x" A_LoopRegName
Layout[Code + 0] := Data
}
Language := {}
loop reg, HKLMSYSTEMCurrentControlSetControlKeyboard Layouts, KVR
{
RegRead Data
if ErrorLevel
Name := "0x" A_LoopRegName
else if (A_LoopRegName = "Layout Text")
Language[Name + 0] := Data
}
return Out := { "Layout":Layout, "Language":Language }
}

Windows允许每个窗口独立地自定义语言设置。如果你不在乎,你可以直接使用活动窗口。您可以使用以下脚本(给出了一些示例)检索语言ID:

SetFormat, Integer, H
WinGet, WinID,, A ;get the active Window
ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
if (InputLocaleID == 0x4070407)
languageName = german
else if (InputLocaleID == 0xF0010409)
languageName = US international
else if (InputLocaleID == 0x4090409 )
languageName = US english
MsgBox, The language of the active window is %languageName%. (code: %InputLocaleID%)

最新更新