AutoHotkey 实现英文输入下句首首字母自动大写
初衷是有些文本编辑器,比如 Texstudio,Notepad 等在英文输入下没有句首首字母大写功能,需要按住 Shift 大写键,很不方便。于是我通过一番摸索,在网上发现 Autohotkey 这个神器。也发现了一个脚本(忘了出处了,囧,大概率是在 AutoHotkey 英文论坛)。这个脚本实现了英文点号、问号、感叹号等接空格或者回车后,下一个英文单词自动首字母大写。但是,可能是由于里面有 loop 的原因,CPU 占用比较高,用起来不舒服。于是抱着试一试的态度在 stackoverflow 上提问,没想到很快就有了回复。[1] 优化后的代码 CPU 和内存占用可以忽略不计。
但是有个问题,就是在中文下,比如微信,QQ 等回车发送消息后,下一次输入汉字汉语拼音时也会大写。而汉字拼音是没有大写开头,这个大写开头的就默认为英文单词了。为了输入汉语必须删掉大写的重来。我用的是小狼毫,没有测试其他的输入法。当然你可以让脚本忽略微信,QQ 等中文软件。但是中英文混合输入也是很有使用场景的。恰好在博客园发现了心如止水的文章,介绍了用 AutoHotkey 来识别输入法。有一段代码可以识别 keyboard layout 是 US keyboard layout 还是中文键盘。[2] 我将他的这个代码"借鉴"到我的代码中,实现忽略中文输入只对英文句首大写。
还有一个小问题,就是在 Windows 下是 Win+ 空格键来切换不同的输入法键盘。我用的是小狼毫,习惯用右 Shift 键来切换中英文。但是 Shift 是小狼毫输入法内部的中英文模式切换,按了 Shift 后没有更换 keyboard layout,所以不能被识别出来。(貌似搜狗和微软拼音能被识别)。于是我又改了一下小狼毫的设置,将左 Shift 键改为小狼毫内部中英文切换。右 Shift 键改为:单击时先输出 左Shift 将打出的字母输出来,然后再输出 Win+ 空格键切换键盘。长按还是原先的功能,比如长按右 Shift 和 a 得到大写字母 A。至此问题解决。附上代码:
#SingleInstance force
#NoEnv
#Persistent
; create a group of the programs in which you want auto-capitalize
GroupAdd, auto_capitalize_group, ahk_exe texstudio.exe
GroupAdd, auto_capitalize_group, ahk_exe chrome.exe
GroupAdd, auto_capitalize_group, ahk_class Framework::CFrame
GroupAdd, auto_capitalize_group, ahk_class Notepad
GroupAdd, auto_capitalize_group, ahk_class WeChatMainWndForPC
GroupAdd, auto_capitalize_group, ahk_exe TIM.exe
SetTimer, auto_capitalize, 300 ; check every 300 ms
Return
auto_capitalize:
If (IMELA_GET()=0x8040804) ; 0x8040804 指代小狼毫中文输入键盘,如果想知道自己的键盘代号,见末尾代码(注释掉了)
return ; do nothing if it is Chinese input keyboard layout
; else:
If !WinActive("ahk_group auto_capitalize_group")
return ; do nothing
; else:
{
Input key, I L1 M V,{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Tab}
StringUpper key, key
If InStr(ErrorLevel,"EndKey")
state =
Else If InStr(".!?",key)
state = 1
Else If InStr("`t `n",key)
{
If state = 1
state = 2
}
Else
{
If state = 2
Send {BS}{%key%}
state =
}
}
Return
Return
; function that detect which keyboard layout is being used
IMELA_GET(){
SetFormat, Integer, H
WinGet, WinID,, A
ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID)
;~ MsgBox, %InputLocaleID%
return %InputLocaleID%
}
; 自定义右Shift键
RShift::
SendInput {RShift Down}
KeyWait, RShift
SendInput {RShift Up}
if ( A_PriorKey = "RShift" )
{
If (IMELA_GET()=0x8040804)
{
SendInput, {LShift}
Sleep, 100
SendInput, #{Space}
Return
}
Else
{
SendInput, #{Space}
Return
}
}
return
; F10:: ; F10 显示输入键盘,我电脑上 US keyboard layout 是 0x4090409 ; SetFormat, Integer, H ; WinGet, WinID,, A ; ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0) ; InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID) ; MsgBox, %InputLocaleID% ; Return
参考文献
- https://stackoverflow.com/questions/61727415/ahk-loop-high-cpu-usage
- https://www.cnblogs.com/xrvu/p/xrzs.html
RShift:: SendInput {RShift Down} KeyWait, RShift SendInput {RShift Up} if ( A_PriorKey = "RShift" ) { If (IMELA_GET()=0x8040804) { SendInput, {LShift} Sleep, 100 SendInput, #{Space} Return } Else { SendInput, #{Space} Return } } return

浙公网安备 33010602011771号