Bruce Simonson
Bruce Simonson

Reputation: 173

python tkinter - detecting arrow keys when an alternate key is pressed

I am using python and tkinter, and wish to capture if an arrow key is pressed, when one of the alternate keys is down.

I have the following working, for detecting shift, or control, but I can't figure out what the the mask needs to be for detecting if an alternate key is down when an arrow key is pressed.

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

Upvotes: 0

Views: 60

Answers (1)

JRiggles
JRiggles

Reputation: 6810

On Windows, I'm seeing 0x20000 for Alt_L and 0x60000 for Alt_R, as reported by this little test app:

import tkinter as tk


def showkey(event) -> None:
    label.config(text=event)


root = tk.Tk()
label = tk.Label(root, text='Press any keys')
label.pack()
root.bind('<KeyPress>', showkey)
root.mainloop()

Upvotes: 3

Related Questions