Edit: Can I detect *any key* being pressed without input or installing any module?

Question:

I am writing a code where the text comes out like this:

import time
def animation(phrase):
  for i in phrase:
    print(i,end="",flush=True)     #Animates text to be printed out one by one
    time.sleep(0.035)
  print(phrase)

While the text is coming out one by one, the user can press enter in between the letters. After, that I have a code that asks for input and it assumes the enter key was the input the user put in.

I have tried using the keyboard module and other modules, I also want to avoid using the input function to detect whether it is an enter key or not.

I want to detect, at any point in my program when the enter key is pressed.

P.S I am using Grok, on online Python platform.

Asked By: TaterTots'

||

Answers:

To detect keyboard input you will need to use the Curses library.
Which is a built-in library in python.

import curses
import os


def main(win):
    win.nodelay(True)
    key = ""
    win.clear()
    win.addstr("Detected key:") # curses version of print

    while True:
        try:
            key = win.getkey() # here it tries to detect the key
            win.clear()
            win.addstr("Detected key:")
            win.addstr(str(key)) #print the key to the display here
            if key == os.linesep:
                break #check where there is a line seperation if so it breaks
        except Exception:
            # No input
            pass #If no key is pressed it just passes and loops again


curses.wrapper(main)

Here is an example where it checks whether a key is pressed and then displays it to the window.

you can edit this to your needs.

Tutorial on Curses Library

Here is a minimal example in curses

Answered By: Sasen Perera

While it "is possible" to do so without installing any extra-library, I’d say, it is possible, of course, but it is complicated:

To be able to read keys without waiting from an enter, requires reconfiguring the terminal device(the bridge between the program that displays a terminal window for you and the sys.stdin and sys.stdout files the Python code "sees"). This reconfiguration takes a lot of parameters, is different across windows and other operating systems, and you have to restore the default configurations after you are done reading the keyboard: it can be a lot of code.

On the other hand, there are libraries that do all that for you. One such library is "terminedia". It is lightweight, currently with one single dependency – it comes with the getch() function that automatically does the configuration/deconfiguration above, and waits for a single key-press in between:

from terminedia import getch
import time

def animation(phrase):
  for i in phrase:
    print(i,end="",flush=True)     #Animates text to be printed out one by one
    getch()  # wait for a keypress
  print(phrase)

(disclaimer: I am terminedia’s author. For other people reaching here in search of non-blocking keyboard reading – there is also terminedia.inkey: won’t block at all waiting for a keypress, and terminedia.ainput: offering an async version of Python input.)

Answered By: jsbueno
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.