pythonloopswhile-loopkeyboard

I want to check if ANY key is pressed, is this possible?


How do I check if ANY key is pressed?

this is how I know to detect one key:

import keyboard  # using module keyboard
while True:  # making a loop
    if keyboard.is_pressed('a'):  # if key 'q' is pressed
        print('You Pressed A Key!')
        break  # finishing the loop 

How do I check if any key (not just letters) is pressed? For example, if someone presses the spacebar it works, the same for numbers and function keys, etc.


Solution

  • while True:
        # Wait for the next event.
        event = keyboard.read_event()
        if event.event_type == keyboard.KEY_DOWN:
            print(event.name) # to check key name
    

    Press any key and get the key name.