How to read a single character from the user?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Fantascape Looping
--
Chapters
00:00 Question
00:27 Accepted answer (Score 230)
01:24 Answer 2 (Score 97)
02:26 Answer 3 (Score 82)
03:05 Answer 4 (Score 77)
04:29 Thank you
--
Full question
https://stackoverflow.com/questions/5103...
Accepted answer links:
[getch()-like unbuffered character reading from stdin on both Windows and Unix]: https://code.activestate.com/recipes/134.../
Answer 2 links:
http://code.activestate.com/recipes/1348.../
Answer 3 links:
[recipe]: http://code.activestate.com/recipes/1348.../
Answer 4 links:
[readchar]: https://github.com/magmax/python-readcha...
[readchar.key.F1]: https://github.com/magmax/python-readcha...
[readchar.config]: https://github.com/magmax/python-readcha...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #input
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Fantascape Looping
--
Chapters
00:00 Question
00:27 Accepted answer (Score 230)
01:24 Answer 2 (Score 97)
02:26 Answer 3 (Score 82)
03:05 Answer 4 (Score 77)
04:29 Thank you
--
Full question
https://stackoverflow.com/questions/5103...
Accepted answer links:
[getch()-like unbuffered character reading from stdin on both Windows and Unix]: https://code.activestate.com/recipes/134.../
Answer 2 links:
http://code.activestate.com/recipes/1348.../
Answer 3 links:
[recipe]: http://code.activestate.com/recipes/1348.../
Answer 4 links:
[readchar]: https://github.com/magmax/python-readcha...
[readchar.key.F1]: https://github.com/magmax/python-readcha...
[readchar.config]: https://github.com/magmax/python-readcha...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #input
#avk47
ACCEPTED ANSWER
Score 239
Here's a link to the ActiveState Recipes site that says how you can read a single character in Windows, Linux and OSX:
getch()-like unbuffered character reading from stdin on both Windows and Unix
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
ANSWER 2
Score 96
sys.stdin.read(1)
will basically read 1 byte from STDIN.
If you must use the method which does not wait for the \n you can use this code as suggested in previous answer:
class _Getch:
"""Gets a single character from standard input. Does not echo to the screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
(taken from http://code.activestate.com/recipes/134892/)
ANSWER 3
Score 82
The ActiveState recipe quoted verbatim in two answers is over-engineered. It can be boiled down to this:
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
getch = _find_getch()
ANSWER 4
Score 22
An alternative method:
import os
import sys
import termios
import fcntl
def getch():
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while 1:
try:
c = sys.stdin.read(1)
break
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
return c
From this blog post.