使用 AHK 检测 Ctrl 键长按

问题描述 投票:0回答:1

我正在使用以下最初由 Laszlo 编写的 AutoHotkey 脚本来检测 Ctrl 键的单次、多次、短按和长按:

Morse(timeout = 150) { ;
    tout := timeout/1000
    key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
    Loop {
        t := A_TickCount
        KeyWait %key%
        Pattern .= A_TickCount-t > timeout
        KeyWait %key%,DT%tout%
        If (ErrorLevel)
            Return Pattern
    }
}

~Ctrl::
    p := Morse()
    If (p = "0")
        MsgBox Short press
    Else If (p = "1")
        MsgBox Long press
    Else If (p = "00")
        MsgBox Two short presses
    Else If (p = "01")
        MsgBox Short+Long presses
    Else
        MsgBox Press pattern %p%
    Return

该脚本可以很好地区分短按和长按 Ctrl。但是,当我将 Ctrl 键与 z、c、v 等其他键一起使用时,我遇到了问题。 在这些情况下,脚本将这些组合检测为长按 Ctrl 键并显示“长按”消息框。 相反,我希望它忽略 Ctrl + 其他键的任意组合并允许默认的系统行为。

感谢修改脚本的任何帮助

autohotkey long-press
1个回答
0
投票
; The keyboard hook must be installed, 
; otherwise the variable A_PriorKey cannot be of use
#InstallKeybdHook

Morse(timeout = 150) { ;
    tout := timeout/1000
    key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
    Loop {
        t := A_TickCount
        KeyWait %key%
        Pattern .= A_TickCount-t > timeout
        KeyWait %key%,DT%tout%
        If (ErrorLevel)
            Return Pattern
    }
}

~Ctrl::
    KeyWait, Ctrl ; waits for the pressed key to be released before the script continues
    ; A_PriorKey is the key pressed prior to the this key-release
    If InStr(A_PriorKey, "Control") ; left or right Control key
        Return ; end this hotkey's subroutine
    ; otherwise
    p := Morse()
    If (p = "0")
        MsgBox Short press
    Else If (p = "1")
        MsgBox Long press
    Else If (p = "00")
        MsgBox Two short presses
    Else If (p = "01")
        MsgBox Short+Long presses
    Else
        MsgBox Press pattern %p%
    Return

参见 A_PriorKey

© www.soinside.com 2019 - 2024. All rights reserved.