英文:
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, '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()
- Using the MouseWheel first will output
None
--as expected. - Using
the Touchpad first will outputTrue
--not expected. - Pressing the
Key will output firstTrue
thenFalse
--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, '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()
I hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论