nutshelluniverse
Posts: 83
Joined: Sun Nov 10, 2013 5:40 pm
Location: UK
Contact: Website

getch() in Python

Sun Feb 09, 2014 2:25 pm

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
HQ9+

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: getch() in Python

Sun Feb 09, 2014 2:37 pm

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'
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

nutshelluniverse
Posts: 83
Joined: Sun Nov 10, 2013 5:40 pm
Location: UK
Contact: Website

Re: getch() in Python

Sun Feb 09, 2014 2:52 pm

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'
Although that does work on Python 2.7, what's the 3.2 equivalent?
HQ9+

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: getch() in Python

Sun Feb 09, 2014 2:58 pm

nutshelluniverse wrote:
Although that does work on Python 2.7, what's the 3.2 equivalent?
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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: getch() in Python

Sun Feb 09, 2014 3:37 pm

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")

nutshelluniverse
Posts: 83
Joined: Sun Nov 10, 2013 5:40 pm
Location: UK
Contact: Website

Re: getch() in Python

Sun Feb 09, 2014 4:19 pm

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")

Oh right.
HQ9+

keybeeper
Posts: 26
Joined: Wed Aug 08, 2012 10:15 am

Re: getch() in Python

Sun Feb 09, 2014 4:41 pm

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
No Microsoft products were used in the creation of this message

stevech
Posts: 144
Joined: Sun Jul 15, 2012 11:53 pm

Re: getch() in Python

Sun Feb 09, 2014 9:25 pm

Opinion: For all but very advanced users, I'd avoid all things Python 3 and stay in the Python 2 world.

keybeeper
Posts: 26
Joined: Wed Aug 08, 2012 10:15 am

Re: getch() in Python

Sun Feb 09, 2014 9:58 pm

Why stay with Python2? Why is it only for very advanced users? I'm a new user and have had no difficulty whatever with Python3. It's virtually the same. In fact I have copied example code written in Python2 into Python3 programs and didn't realise that it was written in Python2. The code worked OK. I prefer to look to the future, not get stuck in the past.
No Microsoft products were used in the creation of this message

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: getch() in Python

Mon Feb 10, 2014 12:49 pm

Even until 6 months ago there were quite important modules that were available for python2 but hadn't yet been converted to work with python3 (and a similar situation applies to pypy) However I think most important things now work in python3 and there are many new modules that won't work in python2, but it takes a while for the world to migrate. For instance the python-imaging library stopped being maintained ages ago, before python3 was sorted so it you wanted to use it for image processing you had to stick to python2. However the Pillow fork of PIL has kept bug fixing and improving the module so it must be better, and it works on python3 and pypy. However on raspbian the decrepit PIL is still automatically included as the graphics module for python, and if you want to apt-get install it you need to point to the 'jessie' repository. (On Arch, interestingly python == python3 and you have to specify python2 if you want that. And you can just pacman -S python-pillow, and Ubuntu is starting to include pillow rather than PIL now I think)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

nutshelluniverse
Posts: 83
Joined: Sun Nov 10, 2013 5:40 pm
Location: UK
Contact: Website

Re: getch() in Python

Sat Mar 01, 2014 10:00 am

So where does the getch() function fit into this?
HQ9+

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: getch() in Python

Sat Mar 01, 2014 12:32 pm

I user curses getch() which seems to work fine with p2 or p3 (is unhappy when run under idle)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

keybeeper
Posts: 26
Joined: Wed Aug 08, 2012 10:15 am

Re: getch() in Python

Sun Mar 02, 2014 8:03 am

As suggested in my previous reply, did you remember to look here?

https://pypi.python.org/pypi/getch
No Microsoft products were used in the creation of this message

Uhgtred
Posts: 1
Joined: Tue Jan 10, 2017 3:42 pm

Re: getch() in Python

Tue Jan 10, 2017 3:45 pm

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
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.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: getch() in Python

Tue Jan 10, 2017 9:16 pm

I think the getch() as used in C will not return until it gets a key press. i.e. it will block the program execution. You could use the curses version I linked to and set the timeout to 0 i.e. like we do here. Or you could do the key press checking in another thread with a loop to set the time a key was pressed. If the time was more than a certain value you could quit. Something along the lines of this completely untested code.

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
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”