1
0
mirror of https://github.com/mnaberez/py65.git synced 2025-03-06 17:29:43 +00:00

Added getch() for blocking character input.

This commit is contained in:
Mike Naberezny 2009-08-18 23:03:37 -07:00
parent bd1aae66e3
commit b122a60ddf

View File

@ -3,11 +3,17 @@ import sys
if sys.platform[:3] == "win":
import msvcrt
def getch(stdin):
""" Read one character from the Windows console, blocking until one
is available. Does not echo the character. The stdin argument is
for function signature compatibility and is ignored.
"""
return msvcrt.getch()
def getch_noblock(stdin):
""" Performs a nonblocking read of one character from the Windows
console and returns it. The stdin argument is for function
signature compatibility and is ignored. If no character is
available, an empty string is returned.
""" Read one character from the Windows console without blocking.
Does not echo the character. If no character is available, an
emptry string is returned.
"""
if msvcrt.kbhit():
return msvcrt.getch()
@ -19,10 +25,24 @@ else:
import termios
import fcntl
def getch(stdin):
""" Read one character from stdin, blocking until one is available.
Does not echo the character.
"""
fd = stdin.fileno()
oldattr = termios.tcgetattr(fd)
newattr = oldattr[:]
newattr[3] &= ~termios.ICANON & ~termios.ECHO
try:
termios.tcsetattr(fd, termios.TCSANOW, newattr)
char = stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldattr)
return char
def getch_noblock(stdin):
""" Performs a nonblocking read of one character from stdin
returns it. If no character is available, an empty string
is returned.
""" Read one character from stdin without blocking. Does not echo the
character. If no character is available, an empty string is returned.
"""
fd = stdin.fileno()