How to make Tkinter run in the background or make it handle keypress events outside of the Tkinter window?

Viewed 50

The idea is that when the user presses the key combination Ctrl+P, Tkinter starts processing keyboard/mouse events regardless of which window is open.

1 Answers

You could use the keyboard module it will get the key even if you minimize the window.

Because inside Tkinter using a loop is not a good practice. This is why I use the root.after method to make a loop.

from tkinter import *
import keyboard

root = Tk()

def loop():

    if keyboard.is_pressed(hotkey='ctrl+p'):
        print('yes')
    root.after(100,loop) # edited
loop()
root.mainloop()

Related