I was wondering if there was a way to detect when a key is pressed in python 3 like below:
if keypressed('a'):
print('you pressed a')
(Example for simplicity's sake)
keyboard
module can give you more than you want.
Here are some several methods to detect keypress:
Method #1:
It will constantly detect your keypress. As you press a
it will print. Press Ctrl+C to break out of the loop
import keyboard
while True:
if keyboard.is_pressed("a"):
print("You pressed 'a'")
Method #2:
It will wait for you to press a
and blocks the whole code until you press a
. It will detect once only.
import keyboard
keyboard.wait('a')
print("You pressed 'a'")