Is there a function that waits for a keypress before continuing in Python 3.2?
I'm looking for something similar to getch() in C or GET/GET$ in BBC BASIC
Code: Select all
#!/usr/bin/python
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
print '\nchar is\'' + ch + '\'\n'Although that does work on Python 2.7, what's the 3.2 equivalent?DougieLawson wrote:Code: Select all
#!/usr/bin/python import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) print '\nchar is\'' + ch + '\'\n'
Sorry, I don't know. I've no desire to move to python3 because too many python2 things are incompatible and most of the free code and code samples out on the Internet is python2.nutshelluniverse wrote:
Although that does work on Python 2.7, what's the 3.2 equivalent?
Code: Select all
def getch():
import sys, tty, termios
old_settings = termios.tcgetattr(0)
new_settings = old_settings[:]
new_settings[3] &= ~termios.ICANON
try:
termios.tcsetattr(0, termios.TCSANOW, new_settings)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(0, termios.TCSANOW, old_settings)
return ch
print("\nchar is '" + getch() + "'\n")jojopi wrote:Python 3 just needs parentheses on the print.
Unless you really need to read control Ctrl+C as a normal character (which makes interrupting the program difficult) it is safer to disable canonical input, rather than setting full raw mode. Also it is good practice to try to restore the terminal settings if something goes wrong.Code: Select all
def getch(): import sys, tty, termios old_settings = termios.tcgetattr(0) new_settings = old_settings[:] new_settings[3] &= ~termios.ICANON try: termios.tcsetattr(0, termios.TCSANOW, new_settings) ch = sys.stdin.read(1) finally: termios.tcsetattr(0, termios.TCSANOW, old_settings) return ch print("\nchar is '" + getch() + "'\n")
what exactly does this return when no key is pressed? i want to abort a programm when no key is pressed and want it running as long as the key is pressed.keybeeper wrote:Anyone learning Python should be encouraged to use Python3. The differences are few and Python3 clears up some of the inconsistencies in Python2. As mentioned by jojopi the main difference you will come across is the print statement. If you are copying Python2 code just put the arguments in parentheses.
Python2 is of the past, Python3 is now and the future.
Simple guide here:
http://www.cs.carleton.edu/faculty/jgol ... on2vs3.pdf
for a getch() function look here:
https://pypi.python.org/pypi/getch
Code: Select all
import time, threading, getch
..
key = 'start'
tm = 0.0
running = True
def check_keys():
global key, tm, running
while running:
key = getch()
tm = time.time()
t = threading.Thread(target=check_keys)
t.start()
while running:
...
# do whatever you program does
if tm - time.time() > 0.5 and key != 'start':
# button not being pressed fast enough
running = False