keyboard_input.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 ## {{{ http://code.activestate.com/recipes/572182/ (r2)
00003 import sys, termios, atexit
00004 from select import select
00005 import roslib; roslib.load_manifest('hrl_lib')
00006 import rospy
00007 
00008 class KeyboardInput():
00009     def __init__(self):
00010 
00011         # save the terminal settings
00012         self.fd = sys.stdin.fileno()
00013         self.new_term = termios.tcgetattr(self.fd)
00014         self.old_term = termios.tcgetattr(self.fd)
00015 
00016         # new terminal setting unbuffered
00017         self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
00018         atexit.register(self.set_normal_term)
00019         self.set_curses_term()
00020 
00021     # switch to normal terminal
00022     def set_normal_term(self):
00023         termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
00024 
00025     # switch to unbuffered terminal
00026     def set_curses_term(self):
00027         termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)
00028 
00029     def putch(self, ch):
00030         sys.stdout.write(ch)
00031 
00032     def getch(self):
00033         return sys.stdin.read(1)
00034 
00035     def getche(self):
00036         ch = self.getch()
00037         self.putch(ch)
00038         return ch
00039 
00040     def kbhit(self):
00041         dr,dw,de = select([sys.stdin], [], [], 0)
00042         return dr <> []
00043 
00044     def pauser(self):
00045         if self.kbhit():
00046             ch = self.getch()
00047             if ch == 'p':
00048                 rospy.loginfo("PAUSED")
00049                 while not rospy.is_shutdown() and ch != 'c':
00050                     ch = self.getch()
00051                     rospy.sleep(0.01)
00052                 rospy.loginfo("CONTINUING")
00053 
00054     def pause(self):
00055         rospy.loginfo("PAUSED")
00056         ch = 'd'
00057         while not rospy.is_shutdown() and ch != 'c':
00058             ch = self.getch()
00059             rospy.sleep(0.01)
00060         rospy.loginfo("CONTINUING")
00061 
00062 if __name__ == '__main__':
00063     ki = KeyboardInput()
00064 
00065     while True:
00066         if ki.kbhit():
00067             ch = ki.getch()
00068             if ch == 'p':
00069                 while ch != 'c':
00070                     ch = ki.getch()
00071             if ch == 'q':
00072                 break
00073         sys.stdout.write('.')
00074 
00075     print 'done'
00076 ## end of http://code.activestate.com/recipes/572182/ }}}
00077 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06