kbhit/getch python equivalent

A

alb

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
 
P

Peter Otten

alb said:
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
 
G

Grant Edwards

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

Chris Angelico

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
 
W

woooee

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top