# MouseDemoGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-10-19 File created. # 2009-02-10 Last modification. # This program demonstrates how to find out which mouse button # is used during mouse operations. In addition this program # checks whether the Control or Shift key is pressed down # simultaneously with a mouse button. # More notes at the end of this file. import gtk class MouseDemoApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "TESTING MOUSE BUTTONS" ) 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( "button_press_event", self.mouse_button_pressed ) self.drawing_area.connect( "motion_notify_event", self.mouse_motion_notified ) self.drawing_area.connect( "button_release_event", self.mouse_button_released ) self.drawing_area.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK ) self.drawing_area.show() self.application_window.show() self.current_mouse_position_x = 40 self.current_mouse_position_y = 40 self.left_mouse_button_is_down = False self.middle_mouse_button_is_down = False self.right_mouse_button_is_down = False self.control_or_shift_key_is_down = False def mouse_button_pressed( self, widget, event ) : self.current_mouse_position_x = int( event.x ) self.current_mouse_position_y = int( event.y ) if event.button == 1 : self.left_mouse_button_is_down = True elif event.button == 2 : self.middle_mouse_button_is_down = True elif event.button == 3 : self.right_mouse_button_is_down = True if event.state & gtk.gdk.CONTROL_MASK != 0 or \ event.state & gtk.gdk.SHIFT_MASK != 0 : self.control_or_shift_key_is_down = True else : self.control_or_shift_key_is_down = False self.drawing_area.queue_draw() # The following method processes the many events that are # generated when the mouse (or pointer) is being moved. # Mostly the system generates only hints about pointer # movements. (This is achieved by specifying # POINTER_MOTION_HINT_MASK in the set_events() call.) # As I understand it, a new hint will be generated only after # the previous hint is processed and get_pointer() method # is called. # The "state" that is mentioned in this method provides # information related to which mouse button is in use. # This information is not, however, exploited. def mouse_motion_notified( self, widget, event ) : if event.is_hint : self.current_mouse_position_x, \ self.current_mouse_position_y, state = event.window.get_pointer() else : # It seems that this else block is never entered. print "A non-hint motion event" self.current_mouse_position_x = int( event.x ) self.current_mouse_position_y = int( event.y ) state = event.state self.drawing_area.queue_draw() def mouse_button_released( self, widget, event ) : if event.button == 1 : self.left_mouse_button_is_down = False elif event.button == 2 : self.middle_mouse_button_is_down = False elif event.button == 3 : self.right_mouse_button_is_down = 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 graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "blue" ) # The following statement draws a body for the mouse. # The mouse body is a large rectangle. this_drawable.draw_rectangle( graphics_context, True, self.current_mouse_position_x - 70, self.current_mouse_position_y - 30, 140, 200 ) if self.control_or_shift_key_is_down == True : graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "red" ) else : graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "yellow" ) if self.left_mouse_button_is_down == True : this_drawable.draw_rectangle( graphics_context, True, self.current_mouse_position_x - 50, self.current_mouse_position_y, 30, 60 ) if self.middle_mouse_button_is_down == True : this_drawable.draw_rectangle( graphics_context, True, self.current_mouse_position_x - 15, self.current_mouse_position_y, 30, 60 ) if self.right_mouse_button_is_down == True : this_drawable.draw_rectangle( graphics_context, True, self.current_mouse_position_x + 20, self.current_mouse_position_y, 30, 60 ) return True def run( self ) : gtk.main() def exit_this_application( self, widget, data=None ) : gtk.main_quit() if __name__ == "__main__": this_application = MouseDemoApplication() this_application.run() # If you want to know how to find out whether the Alt key is used, # please go to # http://www.pygtk.org/pygtk2reference/gdk-constants.html#gdk-modifier-constants # There is no Maemo version of this program simply because, # for the time being, Maemo devices do not provide a mouse. # The stylus that is used in Maemo devices works approximately # like a single-button mouse. # This program works in the same way as MouseDemoApplet.java. # The development of this program has been sponsored # by Nokia Multimedia.