1

My Code:

from tkinter import*
root = Tk()
root.geometry("500x500")
def moti(event):
    root.destroy()
root.bind("<Window>",moti)
root.mainloop()

I want to bind this keyenter image description here

So,how can I bind This key in windows?Thank you!

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Block
  • 159
  • 1
  • 6
  • 3
    The windows key is reserved for Windows to handle. I don't think you can bind to that and it isn't a good idea to bind to it anyways. – TheLizzard Sep 10 '21 at 09:55

1 Answers1

1

From a practical example, I was able to find the windows key is called as <Win_L>(for the left key) <Win_R> for the right one), you can find that out yourself by using this code:

import tkinter as tk

root = tk.Tk()

def event(e):
    print(e.keysym)

root.bind('<Key>',event)
root.mainloop()

This will print the key name once the window has focus and you press on it.

So TL;DR: The key name for the windows key is <Win_L>. Also for reference read keysyms manual page - Tk-built-in-commands

w.bind('<Win_L>',callback)

Note: While on Windows systems you can use <Win_L>, on a ubuntu system it would be <Super_L>. So a safe method would be:

from tkinter import *

root = Tk()

def event(e):
    print(f'You just clicked: {e.keysym}')

try:
    root.bind('<Win_L>',event)
except TclError:
    root.bind('<Super_L>',event)

root.mainloop()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • You might want to mention that it isn't a good idea to bind to the windows key or alt-tab or other events that are supposed to be handled by the OS. – TheLizzard Sep 10 '21 at 10:02
  • @TheLizzard Well maybe not, because these wouldnt stop being handled by the OS, and these just work ONLY when the application has focus too. So I think it's really just personal preference. – Delrius Euphoria Sep 10 '21 at 10:05
  • @Block Anytime, but also keep in mind, it might not be the best of ideas to do this as mentioned above. I have never came across a need where I had to use this binding. – Delrius Euphoria Sep 10 '21 at 10:07
  • Also on Ubuntu 21.04, Python 3.9, `root.bind("", print)` => `_tkinter.TclError: bad event type or keysym "Win_L"` so it's not cross platform? – TheLizzard Sep 10 '21 at 10:08
  • 1
    @TheLizzard Yea unfortunately, I think it is called `` on ubuntu – Delrius Euphoria Sep 10 '21 at 10:09
  • @CoolCloud `root.bind("", print)` doesn't raise any errors but doesn't react to when I press the Windows key. – TheLizzard Sep 10 '21 at 10:11
  • when you click Windows_L button it is showing a menu so how can i block it? – Block Sep 10 '21 at 10:12
  • 2
    @Block I dunno, it is managed by the OS, not sure how you can stop the windows menu to pop up. Now it is getting to bad practices, why would you block the windows menu from popping up. – Delrius Euphoria Sep 10 '21 at 10:14
  • Yeah,I think this is a bad idea.But,I want to block this :) – Block Sep 10 '21 at 10:15
  • @TheLizzard I do not have an ubuntu system to test this code, maybe check [this](https://stackoverflow.com/a/24498250/13382000) – Delrius Euphoria Sep 10 '21 at 10:16
  • @CoolCloud `root.bind("", lambda e: print(e.keysym))` also doesn't catch the event... I don't think the event is even sent to `tkinter`. I think those keys/key combinations should always be left to the OS to handle. – TheLizzard Sep 10 '21 at 10:34
  • @TheLizzard Mmmm, works on windows atleast – Delrius Euphoria Sep 10 '21 at 11:47
  • @CoolCloud On Ubuntu, the windows key opens something like a task bar that can be used to move windows between different desktops. Also pressing windows-x, yields an event but `event.keysym` is just `"x"`. So I think that Ubuntu doesn't send windows key presses to the apps because they shouldn't be using it anyways. – TheLizzard Sep 10 '21 at 11:55
  • @TheLizzard Mmmm – Delrius Euphoria Sep 10 '21 at 12:07