I’m trying to check if the key "q" is pressed whilst running a tkinter loop. Is there a way to do this?
from tkinter import *
import keyboard
def DetectKeyPress():
if keyboard.read_key() == "p":
print("you pressed p!")
root = Tk()
DetectKeyPress()
root.mainloop()
>Solution :
To detect if a key is pressed you can use root.bind('<Key>', function).
Please be aware that pressing shift and some other keys do count as an independent event.
from tkinter import *
def detect_key_press(event):
if event.char == "p":
print("you pressed p!")
root = Tk()
root.bind('<Key>', detect_key_press)
root.mainloop()