Tkinter - CTRL KeyPress 事件通过 TouchPad 滚动触发

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

我试图在按下和释放时跟踪控制键。
虽然一开始一切看起来都很好,但奇怪的事情发生了。
在 Windows 11 上使用我的 HP 笔记本电脑上的触摸板滚动时,KeyPress 事件会自动触发。

虽然我曾经搜索过C代码但没有找到提示,但我可能没有找到正确的文件。这是 Windows 的事情并且是正常行为还是它是 tkinter 中的bug

我的测试代码:

import tkinter as tk

ctrl_pressed = None

def check_ctrl(event):
    print(ctrl_pressed, 'checked')

def track_ctrl(event):
    global ctrl_pressed
    if (et:=event.type.name) == 'KeyPress':
        ctrl_pressed = True
    elif et == 'KeyRelease':
        ctrl_pressed = False
    print(ctrl_pressed, 'tracked')

root = tk.Tk()
root.bind('<MouseWheel>', check_ctrl)
root.bind('<KeyPress-Control_L>', track_ctrl)
root.bind('<KeyRelease-Control_L>', track_ctrl)
root.mainloop()

首先使用 MouseWheel 将输出

None
- 正如预期的那样
先使用触摸板会输出
True
- unexpected
按下 Key 将首先输出
True
然后
False
- 正如预期的那样


好像是一个生成的事件:

def track_ctrl(event):
    print(event.send_event)

用触摸板制作

True


tkinter 的补丁级别

8.6.12

蟒蛇版本
3.11.0

python tkinter tk-toolkit gesture
© www.soinside.com 2019 - 2024. All rights reserved.