# KeyboardInputFullScreenMaemo.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-10-09 File created. # 2009-02-11 Last modification. # This program is an improved version of KeyboardInputDemoMaemo.py. # This version is able to slightly adjust the screen content # when the Full Screen hardware key (F6) is pressed. import gtk import hildon class KeyboardInputFullScreenApplication( hildon.Program ) : def __init__( self ) : hildon.Program.__init__( self ) self.application_window = hildon.Window() 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.drawing_area_exposed ) 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.application_window.connect( "window_state_event", self.window_state_changed ) self.window_in_fullscreen = False self.drawing_area.show() self.application_window.show() self.code_of_last_pressed_key = 63 # The question mark ? def key_pressed( self, widget, event ) : # Let's first check if it is the Full Screen key (F6) if event.keyval == gtk.keysyms.F6 : if self.window_in_fullscreen == True : self.application_window.unfullscreen() else : self.application_window.fullscreen() 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" # A window-state-event is generated after the window # has been "fullscreened" or "unfulscreened". # Here we change the size of the drawing area. # The drawing_area_exposed() method draws a rectangle around # the drawing area so that the current size of the # drawing area is considered. def window_state_changed( self, widget, event ) : if event.new_window_state & gtk.gdk.WINDOW_STATE_FULLSCREEN : self.application_window.set_size_request( 800, 480 ) self.drawing_area.set_size_request( 800, 430 ) self.window_in_fullscreen = True else : self.application_window.set_size_request( 720, 420 ) self.drawing_area.set_size_request( 720, 350 ) self.window_in_fullscreen = False self.drawing_area.queue_draw() def drawing_area_exposed( 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 ) elif self.code_of_last_pressed_key == gtk.keysyms.F6 : text_to_show.set_text( "You pressed the Full Screen (F6) key" ) this_drawable.draw_layout( graphics_context, 100, 250, text_to_show ) # We'll draw a rectangle that makes a boundary around the drawing area. drawing_area_width, drawing_area_height = \ self.drawing_area.get_size_request() this_drawable.draw_rectangle( graphics_context, False, 1, 1, drawing_area_width - 2, drawing_area_height - 2 ) return True def run( self ) : gtk.main() def exit_this_application( self, widget, data=None ) : gtk.main_quit() if __name__ == "__main__": this_application = KeyboardInputFullScreenApplication() this_application.run() # The development of this program has been sponsored # by Nokia Multimedia.