3 # curses_check_for_keypress - loop until user presses a key.
5 # Copyright (C) 2008-2009 William Trevor King
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 # See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 # The author may be contacted at <wking@drexel.edu> on the Internet, or
23 # write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
24 # Philadelphia PA 19104, USA.
27 import curses, curses.ascii
31 # http://www.amk.ca/python/howto/curses/curses.html
41 class check_for_keypress :
42 def __init__(self, prompt="Press any key to continue",
43 timeout_ms=0, test_mode=False) :
44 self.test_mode = test_mode
45 self.last = None # last byte number read
46 self.lasta = None # last as an ASCII character
47 if test_mode == True :
51 # redirect stderr to a file, because exiting curses mode clears
52 # any error messages that had been printed to the screen
53 sys.stderr = StringIO.StringIO()
54 # initialize raw curses mode
55 self.stdscr = curses.initscr()
58 self.stdscr.scrollok(1)
60 self.stdscr.addstr(0,0,prompt+"\n")
62 self.stdscr.nodelay(True)
64 self.stdscr.halfdelay(timeout_ms)
70 if self.test_mode == True :
72 # return to standard terminal
73 while self.input() != None : # eat up the buffer
75 self.stdscr.scrollok(0)
79 # print any errors and restore stderr
80 contents = sys.stderr.getvalue()
81 sys.stderr = sys.__stderr__
82 if len(contents) > 0 :
85 if self.test_mode == True :
91 c = self.stdscr.getch()
96 self.lasta = curses.ascii.unctrl(c)
104 def _output(self, string) :
105 if self.test_mode == True :
108 #y,x = self.stdscr.getyx()
109 self.stdscr.addstr(string)
111 if self.test_mode == True :
113 self.stdscr.refresh()
114 def output(self, string) :
118 def _test_usual_usage() :
119 c=check_for_keypress('testing usual usage, press any key to stop loop...')
121 while c.input() == None :
123 c.output('sleeping\n')
125 raise Exception, "you didn't press a key!"
129 def _test_error_catching() :
130 c=check_for_keypress('testing error catching, wait for the error...')
132 class _testException (Exception):
135 while c.input() == None :
137 print >> sys.stderr, "testing error output"
138 raise _testException, "testing error exception"
140 c.output('sleeping\n')
142 raise Exception, "_test_error_catching() failed!"
143 except _testException, e:
144 assert len(sys.stderr.getvalue()) > 0, "No error message!"
149 _test_error_catching()
151 if __name__ == "__main__" :