独立于键盘的检测shift+digits的方法。

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

我希望能够检测到用户何时按下数字。这一部分是与 <Key-#> 绑定(代码如下).但我需要在用户按下 换挡+数字. 我试过 <Shift-#> 但它不工作,因为按 换挡+数字 将为字符创建相应的事件,如 !, @, #, $, %, ¨, &, *, (, )

人们可以"倾听"但问题是它们在所有键盘上的位置并不一样。

QWERTY - 英文enter image description here

AZERTY -- -- 在比利时部分地区使用。enter image description here

不仅是不同的布局(QWERTY, AZERTY, DVORAK等)不同,但即使是两个不同的 QWERTY 键盘可以根据您所在的地区而有所不同。我的qwerty键盘有一个 ¨ 同调 6 与...相反 ^ 如图)

所以...倾听 对于这样的东西 exclam, dollar, ampersand 和等是一种依赖键盘的检测方式。换挡+数字.

问题:我怎样才能做到与键盘无关的方式?

frame .f -padx 50 -pady 50
focus -force .f
label .f.digit_label -text "Digit:" -padx 50 -pady 20
label .f.keypress_label -text "KeyPress" -padx 50 -pady 20
pack .f
pack .f.digit_label
pack .f.keypress_label
bind .f <Key-1> {::digit_press %K}
bind .f <Key-2> {::digit_press %K}
bind .f <Key-3> {::digit_press %K}
bind .f <Key-4> {::digit_press %K}
bind .f <Key-5> {::digit_press %K}
bind .f <Key-6> {::digit_press %K}
bind .f <Key-7> {::digit_press %K}
bind .f <Key-8> {::digit_press %K}
bind .f <Key-9> {::digit_press %K}
bind .f <Key-0> {::digit_press %K}

bind .f <Shift-1> {::digit_press %K}
bind .f <Shift-2> {::digit_press %K}
bind .f <Shift-3> {::digit_press %K}
bind .f <Shift-4> {::digit_press %K}
bind .f <Shift-5> {::digit_press %K}
bind .f <Shift-6> {::digit_press %K}
bind .f <Shift-7> {::digit_press %K}
bind .f <Shift-8> {::digit_press %K}
bind .f <Shift-9> {::digit_press %K}
bind .f <Shift-0> {::digit_press %K}
bind all <Escape> {exit}
bind all <KeyPress> {::keypress %K}


proc ::digit_press {str} {
    .f.digit_label configure -text "Digit: $str"
}
proc ::keypress {str} {
    .f.keypress_label configure -text "KeyPress: $str"
}
tcl tk
1个回答
1
投票

大多数情况下,在编写软件时,你需要的是按键的熟符号名或它们编码的字符。你的情况则不然。Tk主要向你隐藏了低级的东西,但如果你准备在解码上下功夫,你可以得到它。特别是,如果你在生愿中做到这一点。

bind . <Key> {
    puts "%A:::%s:::%k:::%K"
}

那么你就可以按下按键,看看你能得到什么. 要知道,并不能实际保证这些对所有键盘都是一样的! 在我的键盘上(英国布局),按 1, 2, 3 后跟shift,同样的三个键打印出这样的结果。

1:::0:::26:::1
2:::0:::27:::2
3:::0:::28:::3
{}:::0:::68:::Shift_R
!:::1:::26:::exclam
@:::1:::27:::at
£:::1:::28:::sterling

正如你所看到的 %k 字段报告的键码是 换班时,以及 %s 字段在shift键向下时变成1(这是一个位图,所以用 [expr {%s & 1}] 来测试)。) 你也得把事件扔掉。

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