Skip to content

Instantly share code, notes, and snippets.

@maokwen
Last active February 12, 2025 14:04
Show Gist options
  • Save maokwen/4d99f5c0aa2e7c0c114c708b03fb73ae to your computer and use it in GitHub Desktop.
Save maokwen/4d99f5c0aa2e7c0c114c708b03fb73ae to your computer and use it in GitHub Desktop.
AutoHotKey 锁定微软拼音中英文切换
#Include %A_ScriptDir%
timeInterval := 500
; +-------------------------+-------------------------+
; | SubLanguage ID | Primary Language ID |
; +-------------------------+-------------------------+
; 15 10 9 0 bit
InChs() {
ime_status := DllCall("GetKeyboardLayout", "int", 0, "UInt")
return (ime_status & 0xffff) = 0x804 ; LANGID(Chinese) = 0x804
}
SwitchImeState(id) {
SendMessage(0x283, ; WM_IME_CONTROL
0x002, ; wParam IMC_SETCONVERSIONMODE
1025, ; lParam (Chinese)
, ; Control (Window)
id)
}
DetectHiddenWindows True
SetTimer Mainloop, 1000
MainLoop() {
try {
hWnd := WinGetID("A")
id := DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", hWnd, "Uint")
if (InChs()) {
SwitchImeState(id)
}
}
}
@kwbd
Copy link

kwbd commented Mar 7, 2024

您好,您编写的脚本非常好用,但在使用时我发现了个问题:
脚本中使用的Sleep函数的实现方式可能是阻塞线程,在使用sleep时脚本的CPU占用率会达到3%,我使用SetTimer修改了循环的实现方式,现在CPU占用率显示为0%。
下面是简单的修改(原文件25行之后):

SetTimer Mainloop, 1000

MainLoop() {
  try {
    hWnd := WinGetID("A")
    id := DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", hWnd, "Uint")

    if (InChs()) {
      SwitchImeState(id)
    }
  }
}

@maokwen
Copy link
Author

maokwen commented Mar 7, 2024

@kwbd 谢谢,脚本已经更新

@maokwen
Copy link
Author

maokwen commented Nov 17, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment