切换键盘布局有效,但并非总是如此

时间:2019-11-01 11:38:10

标签: autohotkey

我对Autohotkey有点陌生。

我安装了3种语言,其中一种我很少使用。所以我想使用WIN + Space仅在2种语言(EN-RU)之间切换。 我搜索了在网络上找到的代码,这些代码很短,并且在逻辑上接近我的需求,我做了一些修改以使其更像对象和个性化条件。 代码:全选-展开视图

#Space::
  SetFormat, Integer, H
  Lang := { "EN" : "0x4090409" , "RU" : "0x4190419", "HE": "0x40D040D" }

  WinGet, WinID,, A
  ThreadID:=DllCall("GetWindowThreadProcessId", "Int", WinID, "Int", "0")
  InputLocaleID:=DllCall("GetKeyboardLayout", "Int", ThreadID)

  if(InputLocaleID=Lang.RU OR InputLocaleID=Lang.HE)
    SendMessage, 0x50,, % Lang.EN,, A
  else if(InputLocaleID=Lang.EN)
    SendMessage, 0x50,, % Lang.RU,, A
Exit

它在大多数程序中都能很好地工作,但只有少数程序只能切换到RU。

运行良好:Chrome,notePad,notePad ++,Notion,VScode,Android Studio,IntelliJ IDEA, 不起作用:OneNote Windows 10应用程序(即使在Onenote 2016中也可以正常工作)

如果有人可以帮助我,我将不胜感激。 预先感谢!

1 个答案:

答案 0 :(得分:1)

相关答案Autohotkey Forum

  #Space:: ;  Switch keyboard (EN-RU)

    Lang := { "EN": 0x4090409, "RU": 0x4190419 }
    ; For compatibility with UWP apps, get the thread of the focused
    ; control, not the active window.  This is necessary because those
    ; apps are hosted within a window owned by a different process.
    ControlGetFocus Focused, A
    ControlGet CtrlID, Hwnd,, % Focused, A
    ; Using Ptr vs. Int vs. UInt won't matter in these cases
    ThreadID := DllCall("GetWindowThreadProcessId", "Ptr", CtrlID, "Ptr", 0)
    ; HKL is a handle type (64-bit on x64)
    InputLocaleID := DllCall("GetKeyboardLayout", "UInt", ThreadID, "Ptr")

    if (InputLocaleID != Lang.EN)
      SendMessage, 0x50,, % Lang.EN,, ahk_id %CtrlID%
    else
      SendMessage, 0x50,, % Lang.RU,, ahk_id %CtrlID%
    Exit