alihardan
alihardan

Reputation: 350

How bind/map global hotkey when using Python Tkinter?

I want to bind/map a global hotkey, which also works when application window is minimized and user not focused on it. I'm looking for a cross-platform way. Can I do this using Tkinter?

Upvotes: 2

Views: 2249

Answers (3)

segalion
segalion

Reputation: 11

You can try bindglobal

Install with pip install bindglobal

Has same tkinter bind API with full combinations, but working global Support thread safe and tkinter mainthread

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385950

I want to bind/map a global hotkey, which also works when application window minimized and not focused on it. I'm looking for a cross-platform way.

You can't do that with tkinter. Tkinter has no support for this.

Upvotes: 3

j_4321
j_4321

Reputation: 16169

With tkinter, if the window does not have keyboard focus it won't receive the keypress events so you won't be able to do what you want.

However, with pynput you can listen to the keypress events:

from pynput.keyboard import Listener

def on_press(key):
    print("PRESSED", key)


with Listener(on_press=on_press) as listener:
    listener.join()

This should be cross-platform (but with some patform specific limitations).

Upvotes: 3

Related Questions