0

我想将一个非常具体的事件,即“控制键”后跟另一个“控制键”,类似于 KVM 操作,绑定到一个小部件。

我知道如何绑定各种事件,但不是这个,它包含两个连续事件。

知道如何实施吗?

4

2 回答 2

0

现在处理“双击”所需的快速延迟

bind . <KeyPress> {queueEvent %K}

set queue {}
set times {}

proc queueEvent {key} {
    global queue times
    if {![string match Control* $key]} {set queue {}; set times {}; return}
    if {$key eq $queue} {
        if {[expr [clock milliseconds] - $times] <= 500} {
            puts "double ctrl $key"
            set queue {}; set times {}
            return
        }
    }
    set queue $key
    set times [clock milliseconds]
}
于 2018-12-04T14:46:07.473 回答
0

理论上,您可以使用绑定事件序列来执行此操作:

# The keyboard often has two control keys; they have different names
bind $w <KeyPress-Control_L><KeyPress-Control_L> {puts "double-control (left)"}
bind $w <KeyPress-Control_R><KeyPress-Control_R> {puts "double-control (right)"}

但是,控制键(与其他修饰符一样)以这种方式处理特别复杂,因为操作系统会为您重复它们;他们是特例!因此,您需要构建自己的键盘事件队列:

bind $w <KeyPress> {queueEvent %K}
set queue {no-such-key}
proc queueEvent {key} {
    global queue
    if {[llength [lappend queue $key]] > 2} {
        set queue [lrange $queue end-1 end]
    }
    lassign $queue last this
    if {$last eq $this && [string match Control* $this]} {
        puts "double ctrl"
    }
}

这可能会与其他绑定产生不良交互。如何解决这个问题取决于您的应用程序的更广泛的上下文。

于 2018-12-03T12:00:20.127 回答