Single key press

S

Sergi Pasoev

Hi.

I wonder if it is realistic to get a single key press in Python
without ncurses or
any similar library. In single key press I mean something like j and k
in Gnu less
program, you press the key and and it is captured by the script without need to
press enter afterwards
 
8

88888 Dihedral

Hi.

I wonder if it is realistic to get a single key press in Python
without ncurses or
any similar library. In single key press I mean something like j and k
in Gnu less
program, you press the key and and it is captured by the script without need to
press enter afterwards

Sounds like the fast key searching for a list in the DOS application in the
old days.

This is easy in GUI of just tens of items, but for thousands of items such
as in a directory listing, some API is really slow.
 
8

88888 Dihedral

Hi.

I wonder if it is realistic to get a single key press in Python
without ncurses or
any similar library. In single key press I mean something like j and k
in Gnu less
program, you press the key and and it is captured by the script without need to
press enter afterwards

Sounds like the fast key searching for a list in the DOS application in the
old days.

This is easy in GUI of just tens of items, but for thousands of items such
as in a directory listing, some API is really slow.
 
S

Steven D'Aprano

Hi.

I wonder if it is realistic to get a single key press in Python without
ncurses or
any similar library. In single key press I mean something like j and k
in Gnu less
program, you press the key and and it is captured by the script without
need to press enter afterwards


Coincidentally, I was working on that exact function last night, so you
inspired me to put it up on the Python cookbook. This version should work
on any POSIX system such as Linux, as well as Windows. I don't know if it
will work on Macintosh.

http://code.activestate.com/recipes/577977-get-single-keypress/
 
N

Nobody

I wonder if it is realistic to get a single key press in Python
without ncurses or any similar library.

On Unix, you need to use termios.tcsetattr() to disable "canonical mode".
Otherwise, the tty driver will only pass data up to the application when
the user enters a newline or EOF.

If you do this, you also need to handle SIGTSTP and SIGCONT, restoring the
original terminal settings on SIGTSTP (typically generated by Ctrl-Z) and
restoring the application's setting on SIGCONT.
 
G

Grant Edwards

I wonder if it is realistic to get a single key press in Python
without ncurses or
any similar library.

Yes. Just put the tty associated with stdin in raw mode and make
single byte read() calls on it. Remember to restore the tty settings
when your program exits.
 
8

88888 Dihedral

Yes. Just put the tty associated with stdin in raw mode and make
single byte read() calls on it. Remember to restore the tty settings
when your program exits.

Cheers to virus scan software that has to scan fast and easily for all files
under a drive. Easy clean up in APPS.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top