Tkinter how to bind to shift+tab Tkinter how to bind to shift+tab tkinter tkinter

Tkinter how to bind to shift+tab


The following code (in python 2.7.6) should make it clear:I hope this reference works for you

from Tkinter import *def key(event=None):    print 'You pressed Ctrl+Shift+Tab'root = Tk()frame = Frame(root, width=100, height=100)frame.focus_set()frame.bind('<Control-Shift-KeyPress-Tab>', key)frame.pack()root.mainloop()

EDIT: The above works well for Windows and Mac. For Linux, use

'<Control-ISO_Left_Tab>'.


Not a direct answer and too long for a comment.

You can solve your question by yourself with a simple trick, bind <Key> to a function, and print the key event argument passed to the bind function where you can see which key is pressed or not. Try multiple combinations of keys to see what is the state and what is their keysym or keycode.

import tkinter as tkdef key_press(evt):    print(evt)root = tk.Tk()root.bind("<Key>", key_press)root.mainloop()

It will output the following for pressing SHIFT + TAB combo on macOS.

<KeyPress event state=Shift keysym=Tab keycode=3145753 char='\x19' x=-5 y=-50>

Where,

  • state=Shift means a key event's state is on SHIFT.

  • keysym=Tab means tab key is pressed. If we just press SHIFT, the keysym will be Shift_L or Shift_R (shows Shift_L for both shift keys on mac).

  • keycode is an unique code for each key even for different key combos for example keycode for Left Shift is 131330 and keycode for TAB is 3145737 but when SHIFT + TAB is pressed the code is not the same to either, it is 3145753. (I'm not sure if these are the same code for every os but one can figure it out by printing them to the console)

  • Also, see all the event attributes.

Though bind '<Shift-Tab>' key combination works well on Mac, it can also be used like so...

def key_press(evt):    if evt.keycode==3145753:        print('Shift+Tab is pressed')