You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11触摸板滚动触发Ctrl KeyPress事件的Tkinter问题

触摸板滚动触发Tkinter的Ctrl按键事件疑问

我在追踪Ctrl键的按下与释放事件时遇到了异常情况:在Windows 11的HP笔记本上,使用触摸板滚动会自动触发Ctrl的KeyPress事件,而用鼠标滚轮滚动则不会。想确认这是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()

测试现象

  • 先使用鼠标滚轮,输出None checked——符合预期。
  • 先使用触摸板滚动,输出True tracked后跟着True checked——不符合预期。
  • 手动按下Ctrl键,先输出True tracked,松开后输出False tracked——符合预期。

补充验证

我添加了打印事件属性的代码:

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

使用触摸板滚动时,该代码输出True,说明这是系统生成的合成事件。

环境信息

  • Tkinter版本:8.6.12
  • Python版本:3.11.0

内容的提问来源于stack exchange,提问作者Thingamabobs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:40:58