CTRL键按下事件通过触摸板滚动触发。

huangapple go评论61阅读模式
英文:

CTRL KeyPress event is fired trough scrolling with TouchPad

问题

我试图追踪控制键在按下和释放时的情况。
在一开始一切似乎都很正常,但在我在我的HP笔记本电脑上的Windows 11上使用触摸板滚动时,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”——正如预期的那样。
  • 首先使用触摸板将输出“True”——不符合预期。
  • 按下键将首先输出“True”,然后输出“False”——正如预期的那样。

看起来这是一个生成的事件:

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

这会在触摸板上产生“True”。

我使用的是Tkinter的patchlevel 8.6.12,以及Python版本 3.11.0

英文:

I'm trying to track the control-key when it's pressed and released.
While everything seemed fine in the beginning, but when scrolling with my touch-pad on my HP laptop on Windows 11 the KeyPress event is fired automatically.

Is this a Windows thing and is normal behavior or is it a bug in tkinter?

My code:

import tkinter as tk
    
ctrl_pressed = None
    
def check_ctrl(event):
    print(ctrl_pressed, &#39;checked&#39;)
    
def track_ctrl(event):
    global ctrl_pressed
    if (et:=event.type.name) == &#39;KeyPress&#39;:
        ctrl_pressed = True
    elif et == &#39;KeyRelease&#39;:
        ctrl_pressed = False
    print(ctrl_pressed, &#39;tracked&#39;)
    
root = tk.Tk()
root.bind(&#39;&lt;MouseWheel&gt;&#39;, check_ctrl)
root.bind(&#39;&lt;KeyPress-Control_L&gt;&#39;, track_ctrl)
root.bind(&#39;&lt;KeyRelease-Control_L&gt;&#39;, track_ctrl)
root.mainloop()
  • Using the MouseWheel first will output None--as expected.
  • Using
    the Touchpad first will output True--not expected.
  • Pressing the
    Key will output first True then False--as expected.

It seems to be an generated event:

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

This produces True with touchpad.


> I'm using patchlevel of Tkinter 8.6.12, and Python version 3.11.0

答案1

得分: 2

使用 Windows 10 Pro 和 Python 3.11,我无法复制这个行为。
您可以尝试使用键盘而不是 tkinter 进行事件监听。

以下是使用 keyboard.is_pressed 而不是检查 event.type 的代码示例:

import tkinter as tk
from keyboard import is_pressed
ctrl_pressed = None

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

def track_ctrl(event):
    global ctrl_pressed
    if is_pressed('ctrl'):
        ctrl_pressed = True
    else:
        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()

希望这有所帮助!

英文:

Using Windows 10 pro and Python 3.11 I can't reproduce this behavioure.
You could try using keyboard rather then tkinter for your event listening.

Here is your code with keyboard.is_pressed rather then checking event.type:

import tkinter as tk
from keyboard import is_pressed
ctrl_pressed = None

def check_ctrl(event):
    print(ctrl_pressed, &#39;checked&#39;)

def track_ctrl(event):
    global ctrl_pressed
    if is_pressed(&#39;ctrl&#39;):
        ctrl_pressed = True
    else:
        ctrl_pressed = False
    print(ctrl_pressed, &#39;tracked&#39;)

root = tk.Tk()
root.bind(&#39;&lt;MouseWheel&gt;&#39;, check_ctrl)
root.bind(&#39;&lt;KeyPress-Control_L&gt;&#39;, track_ctrl)
root.bind(&#39;&lt;KeyRelease-Control_L&gt;&#39;, track_ctrl)
root.mainloop()

I hope this helps!

huangapple
  • 本文由 发表于 2023年2月14日 19:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定