Listen For A Single Letter Input When The Terminal Is Open

Question:

I want to make a project which is a bit like a quiz. I want to make it so that instead of typing Y the pressing Enter, I want it to be you press the Y key on your keyboard and wait 5 seconds before the terminal automatically accepts it.

Asked By: Lordcobcob

||

Answers:

This sounds like you want to read keyboard input.
I would check out this page: https://www.delftstack.com/howto/python/python-detect-keypress/

There is a module to detect keyboard input. They also post a nice example:

import keyboard

while True:
    if keyboard.read_key() == "p":
        print("You pressed p")
        break

while True:
    if keyboard.is_pressed("q"):
        print("You pressed q")
        break
        
keyboard.on_press_key("r", lambda _:print("You pressed r"))
Answered By: dominic
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.