S
S
Silence12812021-12-18 15:03:13
Python
Silence1281, 2021-12-18 15:03:13

How to call a function whenever a key is pressed in python?

How to call a function whenever a key is pressed in python?
I have a code that, when one of the keys is pressed, let's say (q, w, e) performed different functions.
(Tkinter is not suitable as it only works within the program window). It is also desirable without using an infinite loop, as it loads the system!

My code (the problem here is that the function is executed 1 time and turns off | how to make it possible to press the key several times and it executed the function)

import keyboard
from threading import *


def q():
    keyboard.wait('q')
    print('q')


def w():
    keyboard.wait('w')
    print('w')


def e():
    keyboard.wait('e')
    print('e')


q = Thread(target=q).start()
w = Thread(target=w).start()
e = Thread(target=e).start()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question