bind Win_L with 'd' in tkinter

Question:

from tkinter import *

def quit_1(event):
    print("you pressed Win_L")
    root.quit()
def quit_2(event):
    print("you pressed Win_L+D")
    root.quit()

root = Tk()
root.bind('<Win_L>', quit_1)     #working
root.bind('<Win_L-D>', quit_2)   #not working
root.mainloop()

How I bind Win_L + D event to a funcction

commenting line
root.bind(‘Win_L-D’,quit_2) runs the program

Asked By: Lakshit Karsoliya

||

Answers:

To bind the Win_L + D key combination to the quit_2 function, you need to modify the event string in the bind method. Instead of using '<Win_L-D>', you need to use '<Win_L>d':

root.bind('<Win_L>', quit_1)
root.bind('<Win_L>d', quit_2)

This is because Win_L is a modifier key and cannot be used in combination with other keys. When you press Win_L + D, the Windows operating system generates a new key code for the combination. In Tkinter, this code is represented by d (the d key), so you need to use it in the event string.
With this change, pressing the Win_L + D key combination will print "you pressed Win_L+D" to the console and quit the root window.

Answered By: kurmo