AHK循环CPU使用率高

社区首页 >问答首页 >AHK循环CPU使用率高问AHK循环CPU使用率高ENStack Overflow用户提问于 2020-05-11 18:21:37回答 1查看 501关注 0票数 0我正在运行一个自动热键脚本,它会自动大写句子的第一个字符(例如,在Texstudio或Chrome中)。脚本(特别是循环)有时会占用30-40%的CPU。因此,我想知道是否有可能优化代码(也许不使用循环?)以减少CPU使用率。提前谢谢。代码如下:

代码语言:javascript复制#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 autohotkeyloopscpu关注问题分享EN回答 1推荐最新Stack Overflow用户发布于 2020-05-11 19:32:23

由于该周期,SetTimer消耗的CPU要少得多。

代码语言:javascript复制#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收藏分享票数 1EN页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持原文链接:https://stackoverflow.com/questions/61727415

复制相关文章