Is there getchar in sage?
Is there a function in sage equivalent to the getchar function from C language?
Is there a function in sage equivalent to the getchar function from C language?
What sort of thing are you trying to do? Maybe there is a more natural way in python than getchar...
To get a user string in python, often the raw_input function is used.
Modify to your tastes:
def key_pressed(self):
"""
Tests whether we received a key press without blocking.
This works by making stdin temporarily non-blocking, and then
reading a single char.
"""
import fcntl
fl = fcntl.fcntl(sys.stdin, fcntl.F_GETFL)
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
key = os.read(sys.stdin.fileno(), 1)
except OSError:
return False
finally:
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fl)
print 'User pressed key '+key
return True
You really want this function in Python, I guess. Here are two relevant links I found, knowing nothing about getchar
; I bet you will be able to do better.
Apparently it's nontrivial to get something that works exactly the same.
Asked: 13 years ago
Seen: 460 times
Last updated: Apr 13 '12