[Python] python tkinter - detecting arrow keys when shift and function key are pressed, or...

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    This is a follow-on question to an earlier question ( python tkinter - detecting arrow keys when an alternate key is pressed )

    I would also like to be able to detect if the function key is pressed, along with the shift, or the control key, while one of the arrow keys (up, down, left, and right) is pressed.

    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")


    I'm hoping there's a pair of 0x???? masks that will make this easy.

    Continue reading...

Compartilhe esta Página