I'm running an Autohotkey script that auto capitalizes the first character of a sentence (for example in Texstudio or Chrome). The script (specifically the loop I guess) sometime causes 30–40% of CPU. Therefore, I am wondering if there is a possibility to optimize the code (maybe without using loop?) to reduce the CPU usage. Thanks in advance. Here is the code:
#SingleInstance force
#NoEnv
SetBatchLines -1
Loop {
if WinActive("ahk_exe texstudio.exe") or WinActive("ahk_exe chrome.exe")
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
SetTimer consumes much less CPU because of the period.
#SingleInstance force
#NoEnv
#Persistent
; SetBatchLines -1
; 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
SetTimer, auto_capitalize, 300 ; check every 300 ms
Return
auto_capitalize:
if !WinActive("ahk_group auto_capitalize_group")
return ; do nothing
; otherwise:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.