Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

kbhit/getch python equivalent

1,207 views
Skip to first unread message

alb

unread,
Apr 22, 2013, 9:34:02 AM4/22/13
to
Hi everyone,

I'm looking for a kbhit/getch equivalent in python in order to be able
to stop my inner loop in a controlled way (communication with external
hardware is involved and breaking it abruptly may cause unwanted errors
on the protocol).

I'm programming on *nix systems, no need to be portable on Windows. I've
seen the msvcrt module, but it looks like is for Windows only.

Any ideas/suggestions?

Al

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Peter Otten

unread,
Apr 22, 2013, 10:00:47 AM4/22/13
to pytho...@python.org
alb wrote:

> I'm looking for a kbhit/getch equivalent in python in order to be able
> to stop my inner loop in a controlled way (communication with external
> hardware is involved and breaking it abruptly may cause unwanted errors
> on the protocol).
>
> I'm programming on *nix systems, no need to be portable on Windows. I've
> seen the msvcrt module, but it looks like is for Windows only.
>
> Any ideas/suggestions?

Curses?

http://docs.python.org/dev/library/curses.html

Grant Edwards

unread,
Apr 22, 2013, 10:10:02 AM4/22/13
to
On 2013-04-22, alb <alessand...@cern.ch> wrote:

> I'm looking for a kbhit/getch equivalent in python in order to be able
> to stop my inner loop in a controlled way (communication with external
> hardware is involved and breaking it abruptly may cause unwanted errors
> on the protocol).
>
> I'm programming on *nix systems, no need to be portable on Windows. I've
> seen the msvcrt module, but it looks like is for Windows only.
>
> Any ideas/suggestions?

Signals, ncurses, termios.

--
Grant Edwards grant.b.edwards Yow! ANN JILLIAN'S HAIR
at makes LONI ANDERSON'S
gmail.com HAIR look like RICARDO
MONTALBAN'S HAIR!

Chris Angelico

unread,
Apr 22, 2013, 11:08:21 AM4/22/13
to pytho...@python.org
On Mon, Apr 22, 2013 at 11:34 PM, alb <alessand...@cern.ch> wrote:
> I'm looking for a kbhit/getch equivalent in python in order to be able
> to stop my inner loop in a controlled way (communication with external
> hardware is involved and breaking it abruptly may cause unwanted errors
> on the protocol).

Catch KeyboardInterrupt and hit Ctrl-C.

ChrisA

woo...@gmail.com

unread,
Apr 22, 2013, 4:30:50 PM4/22/13
to
> I'm looking for a kbhit/getch equivalent in python in order to be able to stop my inner loop in a controlled way (communication with external hardware is involved and breaking it abruptly may cause unwanted errors

A curses example

import curses

stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)

stdscr.addstr(0,10,"Hit 'q' to quit ")
stdscr.refresh()

key = ''
while key != ord('q'):
key = stdscr.getch()
stdscr.addch(20,25,key)
stdscr.refresh()

curses.endwin()

xDog Walker

unread,
Apr 30, 2013, 10:06:34 AM4/30/13
to pytho...@python.org
On Monday 2013 April 22 06:34, alb wrote:
> Hi everyone,
>
> I'm looking for a kbhit/getch equivalent in python in order to be able
> to stop my inner loop in a controlled way (communication with external
> hardware is involved and breaking it abruptly may cause unwanted errors
> on the protocol).
>
> I'm programming on *nix systems, no need to be portable on Windows.

http://code.activestate.com/search/recipes/#q=getch

--
Yonder nor sorghum stenches shut ladle gulls stopper torque wet
strainers.

0 new messages