# KeyboardInputDemoGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-08-31 File created. # 2009-02-11 Last modification. import gtk class KeyboardInputDemoApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "PRESS THE KEYS OF YOUR KEYBOARD" ) self.application_window.connect( "destroy", self.exit_this_application ) self.application_window.set_size_request( 600, 400 ) self.drawing_area = gtk.DrawingArea() self.application_window.add( self.drawing_area ) self.drawing_area.connect( "expose-event", self.handle_expose_event ) self.drawing_area.connect( "key_press_event", self.key_pressed ) self.drawing_area.connect( "key_release_event", self.key_released ) self.drawing_area.set_events( gtk.gdk.EXPOSURE_MASK | gtk.gdk.KEY_PRESS_MASK | gtk.gdk.KEY_RELEASE_MASK ) self.drawing_area.set_flags( gtk.HAS_FOCUS | gtk.CAN_FOCUS ) self.drawing_area.grab_focus() self.drawing_area.show() self.application_window.show() self.code_of_last_pressed_key = 63 # The question mark ? def key_pressed( self, widget, event ) : self.code_of_last_pressed_key = event.keyval print "key press event, CHARACTER: ", print unichr( self.code_of_last_pressed_key ) self.drawing_area.queue_draw() def key_released( self, widget, event ) : print "key release event" def handle_expose_event( self, area, event ) : graphics_context = \ self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window text_to_show = self.drawing_area.create_pango_layout( "" ) # The format specifier %c treats an integer value as a character, # but the integer value must be less than 256. # %s converts an object to a string. # %X shows an integer in hexadecimal form. if self.code_of_last_pressed_key < 256 : text_to_show_as_string = "Last pressed key: %c %X %d" % \ ( self.code_of_last_pressed_key, self.code_of_last_pressed_key, self.code_of_last_pressed_key ) else : text_to_show_as_string = "Last pressed key: %s %X %d" % \ ( self.code_of_last_pressed_key, self.code_of_last_pressed_key, self.code_of_last_pressed_key ) # Let's now convert the string first to a Unicode string and # then to a UTF-8 string. The set_text() method accepts only # a UTF-8 string as its argument. text_to_show_as_utf8_string = \ unicode( text_to_show_as_string, "latin-1" ).encode( "utf8" ) text_to_show.set_text( text_to_show_as_utf8_string ) this_drawable.draw_layout( graphics_context, 100, 200, text_to_show ) if self.code_of_last_pressed_key == gtk.keysyms.F1 : text_to_show.set_text( "You pressed the F1 key" ) this_drawable.draw_layout( graphics_context, 100, 250, text_to_show ) elif self.code_of_last_pressed_key == gtk.keysyms.Up : text_to_show.set_text( "You pressed the Arrow Up key" ) this_drawable.draw_layout( graphics_context, 100, 250, text_to_show ) elif self.code_of_last_pressed_key == gtk.keysyms.Down : text_to_show.set_text( "You pressed the Arrow Down key" ) this_drawable.draw_layout( graphics_context, 100, 250, text_to_show ) return True def run( self ) : gtk.main() def exit_this_application( self, widget, data=None ) : gtk.main_quit() if __name__ == "__main__": this_application = KeyboardInputDemoApplication() this_application.run() # The development of this program has been sponsored by Nokia Multimedia. # This program works in the same way as KeyboardInputDemoApplet.java.