Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pernalin9/220d00016182acb7f13b96cd0506c0a9 to your computer and use it in GitHub Desktop.

Select an option

Save pernalin9/220d00016182acb7f13b96cd0506c0a9 to your computer and use it in GitHub Desktop.
微软输入法自动跳转为中文Fix.ahk
# 如果可以接受capslock作为输入法切换按键,强烈建议使用评论区脚本
# 如果可以接受capslock作为输入法切换按键,强烈建议使用评论区脚本
# 如果可以接受capslock作为输入法切换按键,强烈建议使用评论区脚本
#Include %A_ScriptDir%
timeInterval := 500
; +-------------------------+-------------------------+
; | SubLanguage ID | Primary Language ID |
; +-------------------------+-------------------------+
; 15 10 9 0 bit
InChs(hWnd) {
ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", hWnd, "UInt", 0)
ime_status := DllCall("GetKeyboardLayout", "int", ThreadID, "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
outer:
Loop {
try {
hWnd := WinGetID("A")
id := DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", hWnd, "Uint")
if (InChs(hWnd)) {
SwitchImeState(id)
}
} catch as e {
; ^Esc 开始菜单弹窗,会卡死在找不到当前窗口
continue("outer")
}
Sleep(timeInterval)
}
@silentmoooon
Copy link

我琢磨出了一个新的办法,有兴趣可以试下,就是监听窗口切换事件,刚好有个现成的库(https://github.com/Descolada/AHK-v2-libraries/blob/main/Lib/WinEvent.ahk)可以用,然后代码就很简单了
`#include WinEvent.ahk

WinEvent.Active(ActiveWindowChanged)
Persistent()
ActiveWindowChanged(hook, hWnd, *) {
sleep 100
;ToolTip "激活窗口变了! "
wnd:=DllCall("imm32.dll\ImmGetDefaultIMEWnd", "Uint",WinExist("A"))
DllCall("SendMessage"
, "Ptr", wnd
, "UInt", 0x0283 ;Message : WM_IME_CONTROL
, "UPtr", 0x002 ;wParam : IMC_SETOPENSTATUS
, "Ptr", 1) ;lParam : 0 or 1
}`

其中sleep的时间可能需要根据实际情况调整,因为微软这个傻B切换机制也有一定的延时,在我的电脑上大致试了下,10是肯定切不过来,会被微软的覆盖,50一半一半,100刚好够用,刚好能看到我的设置把微软的给覆盖。

@rzhb
Copy link

rzhb commented Nov 27, 2024

@silentmoooon 发现一个问题,记事本和系统设置无法切换到中文,资源管理器、任务管理器、浏览器都正常,系统 win11 24h2。

@silentmoooon
Copy link

额,确实是,我也不知道为什么, 这两个怎么都不生效,不知道什么时候开始这样的.

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