Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.update_idletasks()


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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading