以下是使用 AutoHotKey 编写的代码,可以实现在激活一个窗口时自动切换为英文输入状态:

#SingleInstance force

SetTitleMatchMode, 2

OnWinActive('SwitchToEnglish')

OnWinDeactivate('SwitchToDefault')

return

SwitchToEnglish()
{
    ; 检查当前输入法是否为英文输入法
    if (GetKeyboardLayout() != '04090409')
    {
        ; 切换为英文输入法
        PostMessage, 0x50, 0, 0x4090409,, A
    }
}

SwitchToDefault()
{
    ; 切换为默认输入法
    PostMessage, 0x50, 0, 0x00000409,, A
}

GetKeyboardLayout()
{
    ; 获取当前活动窗口的句柄
    WinGet, hWnd, ID, A

    ; 获取当前输入法
    hwndIME := DllCall('ImmGetDefaultIMEWnd', 'Ptr', hWnd)
    hwndThread := DllCall('GetWindowThreadProcessId', 'Ptr', hwndIME, 'Ptr', 0)
    hKL := DllCall('ImmGetKeyboardLayout', 'UInt', hwndThread)

    ; 将输入法转换为十六进制字符串
    hKL := Format('0x{:08X}', hKL)

    ; 返回输入法
    return hKL
}

请确保您已经安装了 AutoHotKey,并将上述代码保存为一个 .ahk 文件后,双击运行即可。代码中的 OnWinActive('SwitchToEnglish')OnWinDeactivate('SwitchToDefault') 分别用于在窗口激活和失去焦点时调用相应的函数来切换输入法。

AutoHotKey 自动切换英文输入法 - 窗口激活即生效

原文地址: http://www.cveoy.top/t/topic/ba50 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录