Last active
October 9, 2021 02:12
-
-
Save yuhangch/ecec380c8d01071fcd41f7284cb8620e to your computer and use it in GitHub Desktop.
Auto switch IME for different app && CapsLock > IME switch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SetStoreCapsLockMode False | |
CapsLock:: | |
{ | |
KeyWait "CapsLock" | |
time := A_TimeSinceThisHotkey | |
if (time > 300) { | |
SetTimer SendCaps, -4 | |
} | |
else { | |
Send "#{Space}" | |
} | |
SendCaps() | |
{ | |
Send "{CapsLock}" | |
} | |
} | |
;获取当前激活窗口所使用的IME的ID | |
zh := DllCall("LoadKeyboardLayout", "Str", "00000804", "Int", 1) | |
en := DllCall("LoadKeyboardLayout", "Str", "00000409", "Int", 1) | |
getCurrentIMEID(){ | |
winID:=winGetID("A") | |
ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0) | |
InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt") | |
return InputLocaleID | |
} | |
; 使用IMEID激活对应的输入法 | |
switchIMEbyID(IMEID){ | |
winTitle:=WinGetTitle("A") | |
PostMessage(0x50, 0, IMEID,, WinTitle) | |
; Send "{Shift}" | |
} | |
opened := Map() | |
; 使用窗口组实现批量窗口的监视 | |
GroupAdd "enAppGroup", "ahk_exe pwsh.exe" ;添加powershell | |
GroupAdd "enAppGroup", "ahk_exe Code.exe" ;添加vscode | |
GroupAdd "enAppGroup", "ahk_exe WindowsTerminal.exe" ;添加windows terminal | |
; 循环等待知道窗口组的窗口激活,切换当前输入法为en,之后再等待当切换出当前窗口继续监视 | |
Loop{ | |
WinWaitActive "ahk_group enAppGroup" | |
currentWinID := WinGetID("A") | |
if not opened.Has(currentWinID) { | |
opened[currentWinID] := 1 | |
switchIMEbyID(en) | |
} | |
For winId in opened | |
if not WinExist(winId) { | |
opened.Delete(winId) | |
} | |
WinWaitNotActive(currentWinID) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment