loops - AHK 循环 CPU 使用率过高
问题描述
我正在运行一个自动大写句子的第一个字符的 Autohotkey 脚本(例如在 Texstudio 或 Chrome 中)。脚本(特别是我猜的循环)有时会占用 30-40% 的 CPU。因此,我想知道是否有可能优化代码(可能不使用循环?)以减少 CPU 使用率。提前致谢。这是代码:
#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消耗的 CPU 少得多。
#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
推荐阅读
- python - 使用 smtplib 发送内存中的 csv 附件
- c - 创建一个包含给定空格数的字符串
- php - MySql datetime:在 DateTime::DATE_ATOM 中抛出日期错误
- jsf - 如何制作primefaces的响应对话框
- excel - VBA-一次复制多个图表
- javascript - 从 ui 选择选项中删除对象
- angular - 以角度上传文件时出现错误“不支持的内容类型:应用程序/vnd.ms-excel”
- c# - 将不同的项目添加到 DataGridView 中每一行的 DataGridViewComboBoxColumn
- android - Android DiffUtil.ItemCallback areContentsTheSame 不同的ID
- reactjs - React keydown 事件捕获和冒泡