Polling from keyboard

S

sturnfie

I am trying to find a way to poll the keyboard. In my searching, I
have found that Windows users are given the msvcrt module. Is there an
equivilant for Unix systems?

I am writing a p2p chat application, and would like to ideally approach
user input in a manner similar to the way I am using poll() to moniter
the sockets for events

thanks in advance
 
D

Diez B. Roggisch

I am trying to find a way to poll the keyboard. In my searching, I
have found that Windows users are given the msvcrt module. Is there an
equivilant for Unix systems?

I am writing a p2p chat application, and would like to ideally approach
user input in a manner similar to the way I am using poll() to moniter
the sockets for events

I currently do that under Linux using /dev/input/*. However, that's for a
special case where we want several keyboards read simultaneously.

I guess what you want is a RAW-mode for your terminal, or alternatively the
event-system of your windowing system. Then you can get every keystroke.
But we need somewhat more information to be more helpful I fear.

diez
 
P

Petr Jakes

I am using following code which I have found on
http://www.ibiblio.org/obp/py4fun/ few months ago. It works well for my
purposes. Another way is to use Curses library.
HTH
Petr Jakes

#!/usr/local/bin/python
#
# t t y L i n u x . p y
#
# getLookAhead reads lookahead chars from the keyboard without
# echoing them. It still honors ^C etc
#
import termios, sys, time
if sys.version > "2.1" : TERMIOS = termios
else : import TERMIOS

def setSpecial () :
"set keyboard to read single chars lookahead only"
global oldSettings
fd = sys.stdin.fileno()
oldSettings = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ECHO # lflags
new[3] = new[3] & ~TERMIOS.ICANON # lflags
new[6][6] = '\000' # Set VMIN to zero for lookahead only
termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)

def setNormal () :
"restore previous keyboard settings"
global oldSettings
fd = sys.stdin.fileno()
termios.tcsetattr(fd, TERMIOS.TCSADRAIN, oldSettings)

def readLookAhead () :
"read max 1 chars (arrow escape seq) from look ahead"
return sys.stdin.read(1)
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top