[Jahshaka-cvs] jah/jahtools/jbatch keygrab.py,NONE,1.1
Status: Beta
Brought to you by:
jahshaka
From: Charles Y. <lil...@us...> - 2007-03-09 10:40:06
|
Update of /cvsroot/jahshaka/jah/jahtools/jbatch In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv15820 Added Files: keygrab.py Log Message: + Initial key grabber code (untested on windows) --- NEW FILE: keygrab.py --- import platform import atexit if platform.system( ) == "Windows": import msvcrt class keygrab: def __init__( self ): pass def get( self ): if msvcrt.kbhit( ): return msvcrt.getch( ) else: import termios, sys, os class keygrab: def __init__( self ): self.fd = sys.stdin.fileno( ) self.old = termios.tcgetattr( self.fd ) self.new = termios.tcgetattr( self.fd ) self.new[ 3 ] = self.new[ 3 ] & ~termios.ICANON & ~termios.ECHO self.new[ 6 ][ termios.VMIN ] = 0 self.new[ 6 ][ termios.VTIME ] = 0 termios.tcsetattr( self.fd, termios.TCSANOW, self.new ) def __del__( self ): termios.tcsetattr( self.fd, termios.TCSAFLUSH, self.old ) def get( self ): return os.read( self.fd, 1 ) grab = None def init( ): keygrab.grab = keygrab( ) atexit.register( destroy ) def used( ): return keygrab.grab is not None def destroy( ): keygrab.grab = None def get( ): if keygrab.grab is not None: return keygrab.grab.get( ) return '' def main( ): init( ) while 1: key = get( ) if key != '': print "key = ", key if key == 'q': break if __name__ == "__main__": main( ) |