autoSwitcherIME3.ahk
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; AHK版本: 1.1.23.01
; 语言: 中文
; 作者: lspcieee <lspcieee@gmail.com>
; 网站: http://www.lspcieee.com/
; 脚本功能: 自动切换输入法
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;=====分组配置
;中文输入法的分组
GroupAdd,cn,ahk_exe QQ.exe ;QQ
GroupAdd,cn,ahk_exe WINWORD.EXE ;word
GroupAdd,cn,ahk_exe MindManager.exe
;英文输入法的分组
GroupAdd,en,ahk_exe devenv.exe ;Visual Studio
GroupAdd,en,ahk_exe dopus.exe
GroupAdd,en,ahk_class Notepad++
;编辑器分组
GroupAdd,editor,ahk_exe devenv.exe ;Visual Studio
GroupAdd,editor,ahk_exe notepad.exe ;记事本
GroupAdd,editor,ahk_class Notepad++
;函数
;从剪贴板输入到界面
sendbyclip(var_string)
{
ClipboardOld = %ClipboardAll%
Clipboard =%var_string%
ClipWait
send ^v
sleep 100
Clipboard = %ClipboardOld% ; Restore previous contents of clipboard.
}
setChineseLayout(){
;发送中文输入法切换快捷键,请根据实际情况设置。
send {Ctrl Down}{Shift}
send {Ctrl Down},
send {Ctrl Down}{Shift}
send {Ctrl Down},
send {Ctrl Up}
}
setEnglishLayout(){
;发送英文输入法切换快捷键,请根据实际情况设置。
send {Ctrl Down}{Shift}
send {Ctrl Down},
send {Ctrl Down}{Shift}
send {Ctrl Down},
send {Ctrl Down}{Space}
send {Ctrl Up}
}
;监控消息回调ShellMessage,并自动设置输入法
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage")
ShellMessage( wParam,lParam ) {
If ( wParam = 1 )
{
WinGetclass, WinClass, ahk_id %lParam%
;MsgBox,%Winclass%
Sleep, 1000
WinActivate,ahk_class %Winclass%
;WinGetActiveTitle, Title
;MsgBox, The active window is "%Title%".
IfWinActive,ahk_group cn
{
setChineseLayout()
TrayTip,AHK, 已自动切换到中文输入法
return
}
IfWinActive,ahk_group en
{
setEnglishLayout()
TrayTip,AHK, 已自动切换到英文输入法
return
}
}
}
;在所有编辑器中自动切换中英文输入法
#IfWinActive,ahk_group editor
:*:// ::
;//加空格 时 切换到中文输入法
setEnglishLayout()
sendbyclip("//")
setChineseLayout()
return
:Z*:///::
;///注释时 切换到中文输入法(也可以输入///加空格)
setEnglishLayout()
sendbyclip("//")
SendInput /
setChineseLayout()
return
:*:" ::
;引号加空格 时 切换到中文输入法
setEnglishLayout()
SendInput "
setChineseLayout()
return
:*:`;`n::
;分号加回车 时 切换的英文输入法
setEnglishLayout()
sendbyclip(";")
SendInput `n
return
:Z?*:`;`;::
;两个分号时 切换的英文输入法
setEnglishLayout()
return
:Z?*: ::
;输入两个空格 切换的中文输入法
setEnglishLayout()
setChineseLayout()
return
#IfWinActive
autoSwitchIME2.ahk
Gui, Add, GroupBox, x6 y4 w230 h10 , 已安装的输入法(双击切换)
Gui, Add, ListView, r20 x6 y24 w230 h120 vListIME gSetIME ,序号|键盘布局|名称
Gui, Add, Button, x6 y144 w80 h30 gPreIME, 上一输入法
Gui, Add, Button, x156 y144 w80 h30 gNextIME, 下一输入法
Gui, Add, Button, x86 y144 w70 h30 gStateIME, 当前状态
; Generated using SmartGUI Creator 4.0
Gui, Show, x397 y213 h190 w247,输入法切换
Gosub,ReadIME
Return
GuiClose:
ExitApp
ReadIME:
LV_ModifyCol(3,300)
Loop,HKEY_USERS,.DEFAULT/Keyboard Layout/Preload, 1, 1
{
RegRead,Layout
RegRead,IMEName,HKEY_LOCAL_MACHINE,SYSTEM/CurrentControlSet/Control/Keyboard Layouts/%Layout%,Layout Text
RegRead,Layout
ListContent=%A_LoopRegName%丨%IMEName%丨 %Layout%
LV_Insert(1,"Vis",A_LoopRegName,Layout,IMEName)
}
Return
StateIME:
Result:=DllCall("GetKeyboardLayout","int",0,UInt)
SetFormat, integer, hex
Result += 0
SetFormat, integer, D
MsgBox 当前键盘布局为 %Result%
return
SetIME:
If (A_GuiEvent<>"DoubleClick")
{
Return
}
Gui,Submit,Nohide
LV_GetText(Layout,A_EventInfo,2)
;~ MsgBox %Layout%
SwitchIME(Layout)
Return
SwitchIME(dwLayout)
{
DllCall("SendMessage", UInt, WinActive("A"), UInt, 80, UInt, 1, UInt, DllCall("LoadKeyboardLayout", Str, dwLayout, UInt, 1))
}
NextIME:
DllCall("SendMessage", UInt, WinActive("A"), UInt, 80, UInt, 1, UInt, DllCall("ActivateKeyboardLayout", UInt, 1, UInt, 256))
;-- 对当前窗口激活下一输入法
Return
PreIME:
DllCall("SendMessage", UInt, WinActive("A"), UInt, 80, UInt, 1, UInt, DllCall("ActivateKeyboardLayout", UInt, 0, UInt, 256))
;-- 对当前窗口激活上一输入法
Return
autoSwitchIME.ahk
#Persistent
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage")
Return
ShellMessage( wParam,lParam ) {
If ( wParam = 1 )
{
WinGetclass, WinClass, ahk_id %lParam%
;希望切到中文输入法的窗口类名
If Winclass in Notepad2U
{
winget,WinID,id,ahk_class %WinClass%
SetLayout("E0200804",WinID)
;系统内安装的输入法代码可以在注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layouts查到
;将0200804替换为你期望切换的输入法代码即可
}
If Winclass in ConsoleWindowClass,ListarySearchBox ;需要关闭中文输入的窗口类名
{
winget,WinID,id,ahk_class %WinClass%
SetLayout("00000804",WinID)
;00000804是english代码,系统内安装的输入法代码可以在注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layouts查到
}
; If Winclass in Notepad2U ;需要中文输入法英文状态的,窗口类名
; {
; winget,WinID,id,ahk_class %WinClass%
; SetLayout("E0200804",WinID)
; SendInput, {Shift}
; }
}
}
;设置的函数
SetLayout(Layout,WinID){
DllCall("SendMessage", "UInt", WinID, "UInt", "80", "UInt", "1", "UInt", (DllCall("LoadKeyboardLayout", "Str", Layout, "UInt", "257")))
}