按次数(单击与多击)
以常见的双击为例,双击实际上为短时间内两次连续的单击,包含了两个必要条件:
- 两次单击的时间间隔必须在短时间内
- 两次单击必须是连续的
Code: Select all
intInterval := 500 ; 若两次连击在这个时间间隔中,则视为双击。
~RControl::
if (A_PriorHotkey <> "~RControl" or A_TimeSincePriorHotkey > intInterval)
{
KeyWait, RControl
return
}
MsgBox, 您双击了右边的 Ctrl 键。
return
- 减少对该键单击的影响
- 减少对该键其他组合键的影响
既然双击可行,那么三击或更多呢?在实际中不常用,不过思考还是挺有趣的。下面是多次连击的实用例子,设计的很精巧:
Code: Select all
CapsLock::
if (A_ThisHotkey <> A_PriorHotkey)
return
intCount := (intCount = "" ? 1 : intCount + 1)
SetTimer, MultiPresses, -500
Return
; 这里根据连续按下次数判断执行的操作。
MultiPresses:
if (intCount = 1)
MsgBox, 这是单击。
else if (intCount = 2)
MsgBox, 这是双击。
else if (intCount = 3)
MsgBox, 这是三次连击。
intCount := "" ; 重置记数器。
Return
Code: Select all
#c::
if (intCount > 0) ; SetTimer 已经启动,这里记录键击。
{
intCount += 1
return
}
intCount = 1
SetTimer, KeyWinC, 400 ; 在 400 毫秒内等待更多的键击。
return
KeyWinC:
SetTimer, KeyWinC, off
if (intCount = 1) ; 此键按下了一次。
{
Run, m:\
}
else if (intCount = 2) ; 此键按下了两次。
{
Run, m:\multimedia
}
else if (intCount > 2)
{
MsgBox, 连击了三次或更多。
}
intCount = 0
return
前两个例子尽管看起来长一些,实际上很好理解,但下面这个虽短却需好好思考及实践才会有点头绪:
Code: Select all
#MaxThreadsPerHotkey 5
#Space:: ; Win+Space 热键.
#MaxThreadsPerHotkey 1
intCount := (intCount ? (intCount + 1) : 1)
Sleep, 1000
if (intCount = 1)
MsgBox, 这是单击。
else if (intCount = 2)
MsgBox, 这是双击。
else if (intCount = 3)
MsgBox, 这是三次连击。
intCount := "" ; 重置记数器。
Return
这里举这么些例子(还有其他花样),不是为了看起来多厉害,而是在实际中经常必须有所选择。很多人不理解,为什么需要 SendRaw / SendInput / SendPlay / SendEvent 那么多类似的命令呢?在应用时实际情况比较复杂,而这些情况很难在程序中自动做出最优选择,所以需要平时多使用多实践才会找到感觉:什么情况下使用哪个会更有效(这是最让初学者困惑的问题之一)。
按时长
根据按住按键的时长执行不同的操作,可能实际中比上一种用法更少见。
Code: Select all
Esc:: ; 在按下时触发。
If StartTime
return
StartTime := A_TickCount
return
Esc up:: ; 在弹起时触发。
TimeLength := A_TickCount - StartTime
if (TimeLength < 200)
{
MsgBox, 您按住退出键不到 200 毫秒。
}
else if (TimeLength < 1500 and TimeLength >= 200) ; 后一条件实际是多余的,加上只是为了更清晰。
{
MsgBox, 您按住退出键 1 秒左右。
}
StartTime := ""
return
Code: Select all
Esc::StartTime := A_TickCount
Code: Select all
Esc:: ; 在按下时触发。
If StartTime
return
StartTime := A_TickCount
Hotkey, Esc up, EscUpSub, On
return
EscUpSub:
Hotkey, Esc up, EscUpSub, Off
TimeLength := A_TickCount - StartTime
if (TimeLength < 200)
{
MsgBox, 您按住退出键不到 200 毫秒。
}
else if (TimeLength >= 200 and TimeLength < 1500)
{
MsgBox, 您按住退出键 1 秒左右。
}
StartTime := ""
return