( python tkinter — обнаружение клавиш со стрелками при нажатии альтернативной клавиши)
Я также хотел бы иметь возможность чтобы определить, нажата ли функциональная клавиша вместе с клавишей Shift или клавишей управления при нажатии одной из клавиш со стрелками (вверх, вниз, влево и вправо).
Код: Выделить всё
import tkinter
def on_key_press(event):
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 & 0x????: # shift key and function key 0x????
if event.keysym == 'Down':
print("shift and function and down arrow")
elif event.keysym == 'Up':
print("shift and function and up arrow")
elif event.keysym == 'Left':
print("shift and function and left arrow")
elif event.keysym == 'Right':
print("shift and function 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????: # control key and function key 0x????
if event.keysym == 'Down':
print("control and function and down arrow")
elif event.keysym == 'Up':
print("control and function and up arrow")
elif event.keysym == 'Left':
print("control and function and left arrow")
elif event.keysym == 'Right':
print("control and function and right arrow")
elif event.state & 0x20000: # alternate key
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")
ОБНОВЛЕНИЕ после превосходного комментария Дерека ниже.
Как это происходит , на моем ноутбуке Lenovo есть клавиша «Fn» на левой стороне клавиатуры, но эта клавиша не работает так же, как клавиши Shift, Control или альтернативные клавиши.
В частности, эта клавиша Fn не генерирует нажатия клавиш, такие как «ctrl-q» или «shift-A» (например)... то есть (например) не обнаружено клавишного события для «Fn-q» или « Fn-A" на этой клавиатуре.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -pressed-o