【问题标题】:How do you get the current keyboard layout (language setting) in AutoHotkey如何在 AutoHotkey 中获取当前的键盘布局(语言设置)
【发布时间】:2021-10-21 19:03:53
【问题描述】:

我希望能够让我的脚本对当前的键盘布局敏感。类似的东西

#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

【问题讨论】:

    标签: keyboard autohotkey


    【解决方案1】:

    Windows 允许为每个窗口独立地进行自定义语言设置。如果你不在乎,你可以只使用活动窗口。您可以使用此脚本检索Language 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%)
    

    【讨论】:

      【解决方案2】:

      @Cadoiz pointed you in the right directionAccording to MSDN返回的值其实是二:

      要加载的输入语言环境标识符的名称。该名称是由语言标识符(低位字)的十六进制值和设备标识符(高位字)组成的字符串。例如,美国英语的语言标识符为 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, HKLM\SYSTEM\CurrentControlSet\Control\Keyboard Layout\DosKeybCodes
          {
              RegRead Data
              Code := "0x" A_LoopRegName
              Layout[Code + 0] := Data
          }
          Language := {}
          loop reg, HKLM\SYSTEM\CurrentControlSet\Control\Keyboard 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 }
      }
      

      【讨论】:

      • 谢谢你的好回答——你是怎么做这样的语法高亮的?
      • 我找到了,你用cpp语法高亮。
      • 有点作弊,但胜过纯文本。
      猜你喜欢
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多