我正在使用 python 和 tkinter,并且希望在按下备用键之一时捕获是否按下箭头键。
我有以下工作,用于检测移位或控制,但我无法弄清楚掩码需要是什么来检测按下箭头键时是否按下了备用键。
if event.keysym == 'space':
print("space bar")
if event.state & 0x0001: # shift key
if event.keysym == 'Down':
print("shift and down arrow")
elif event.keysym == 'Up':
print("shift and up arrow")
elif event.keysym == 'Left':
print("shift and left arrow")
elif event.keysym == 'Right':
print("shift and right arrow")
elif event.state & 0x0004: # control key
if event.keysym == 'Down':
print("control and down arrow")
elif event.keysym == 'Up':
print("control and up arrow")
elif event.keysym == 'Left':
print("control and left arrow")
elif event.keysym == 'Right':
print("control and right arrow")
elif event.state & 0x????: # alternate key --- 0x????
if event.keysym == 'Down':
print("alternate and down arrow")
elif event.keysym == 'Up':
print("alternate and up arrow")
elif event.keysym == 'Left':
print("alternate and left arrow")
elif event.keysym == 'Right':
print("alternate and right arrow")
在 Windows 上,我看到
0x20000
代表 Alt_L
,0x60000
代表 Alt_R
,正如这个小测试应用程序所报告的:
import tkinter as tk
def showkey(event) -> None:
label.config(text=event)
root.update_idletasks()
root = tk.Tk()
label = tk.Label(root, text='Press any keys')
label.pack()
root.bind('<KeyPress>', showkey)
root.mainloop()