Skip to content
← Back to Community
Live python keyboard input without having to hit enter.
Profile icon
DragoniteDL

I'm trying to create a game (without pygame) using the print() command, but need a live input without having to hit enter! (If possible, I'd like a simple answer...)

Answered by ArchieMaclean [earned 5 cycles]
View Answer
Voters
Profile icon
RyanTang11
Comments
hotnewtop
Profile icon
ArchieMaclean

Refer to this stackoverflow question.
Please upvote if this helps :)

Profile icon
themaka

@ArchieMaclean You had the link and text reverse in your link -- it's the string you want displayed in brackets followed by the link in parenthesis.

Profile icon
ArchieMaclean

@themaka it was like that I think....

Profile icon
A1p1

use curses

Profile icon
RyanTang11
from getkey import getkey key = getkey()
Profile icon
eidhernan

You can use the blessed library. Look up "blessed" in Packages, and install it.

The library actually supports color too, but I made a function to make things easier:

from blessed import Terminal t = Terminal() def wait(timeout_ms =None): alphabet = "abcdefghijklmnopqrstuvwxyz1234567890" keycodes = { " ":"KEY_SPACE", ".":"KEY_PERIOD", ",":"KEY_COMMA", "/":"KEY_SLASH", ";":"KEY_SEMICOLON", "'":"KEY_QUOTE", "`":"KEY_TICK", "-":"KEY_HYPHEN", "=":"KEY_EQUALS", "[":"KEY_OPENBRACE", "]":"KEY_CLOSEBRACE", "\\":"KEY_BACKSLASH", "+":"KEY_ADD", "*":"KEY_ASTERISK" } if timeout_ms != None: timeout_ms = timeout_ms / 1000 with t.cbreak():#wait for keyboard input k = t.inkey(timeout=timeout_ms) if k._name != None: if "KEY_" in k._name: return k._name elif k in alphabet: return "KEY_" +k.upper() elif k in keycodes: return keycodes[k]