# MouseDemoQt.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2009-11-30 File created. # 2010-09-22 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. # http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmouseevent.html import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MouseDemoWindow( QWidget ) : def __init__( self, parent = None ) : QWidget.__init__( self, parent ) self.setGeometry( 200, 200, 680, 480 ) self.setWindowTitle( "TESTING MOUSE BUTTONS" ) self.current_cursor_position_x = 340 self.current_cursor_position_y = 120 self.left_mouse_button_is_down = False self.middle_mouse_button_is_down = False self.right_mouse_button_is_down = False self.control_key_is_down = False self.shift_key_is_down = False # As mouse tracking is set on with the following statement, # MouseMOVeEvents are generated also when the mouse is moved # without pressing its buttons. self.setMouseTracking( True ) # The following statement may help to get keyboard input # to this window. self.setFocusPolicy( Qt.StrongFocus ) def mousePressEvent( self, event ) : self.current_cursor_position_x = event.x() self.current_cursor_position_y = event.y() if event.button() == Qt.LeftButton : self.left_mouse_button_is_down = True elif event.button() == Qt.MidButton : self.middle_mouse_button_is_down = True elif event.button() == Qt.RightButton : self.right_mouse_button_is_down = True self.update() def mouseMoveEvent( self, event ) : self.current_cursor_position_x = event.x() self.current_cursor_position_y = event.y() self.update() def mouseReleaseEvent( self, event ) : if event.button() == Qt.LeftButton : self.left_mouse_button_is_down = False elif event.button() == Qt.MidButton : self.middle_mouse_button_is_down = False elif event.button() == Qt.RightButton : self.right_mouse_button_is_down = False self.update() def keyPressEvent( self, event ) : if event.key() == Qt.Key_Control : self.control_key_is_down = True elif event.key() == Qt.Key_Shift : self.shift_key_is_down = True def keyReleaseEvent( self, event ) : if event.key() == Qt.Key_Control : self.control_key_is_down = False elif event.key() == Qt.Key_Shift : self.shift_key_is_down = False def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) # We'll change the backround by filling the entire # window area with a new color painter.fillRect( QRect( 0, 0, self.width(), self.height() ), QColor( 0xC8, 0xDC, 0xFF ) ) # light blueish painter.setBrush( Qt.darkGray ) # The following statement draws a body for the mouse. # The mouse body is a large ellipse. painter.drawEllipse( self.current_cursor_position_x - 80, self.current_cursor_position_y - 30, 160, 210 ) if self.control_key_is_down == True or \ self.shift_key_is_down == True : painter.setBrush( Qt.magenta ) ; else : painter.setBrush( Qt.yellow ) ; if self.left_mouse_button_is_down == True : painter.drawEllipse( self.current_cursor_position_x - 55, self.current_cursor_position_y + 5, 30, 60 ) if self.middle_mouse_button_is_down == True : painter.drawEllipse( self.current_cursor_position_x - 15, self.current_cursor_position_y - 5, 30, 60 ) if self.right_mouse_button_is_down == True : painter.drawEllipse( self.current_cursor_position_x + 25, self.current_cursor_position_y + 5, 30, 60 ) painter.end() # The main program begins. this_application = QApplication( sys.argv ) application_window = MouseDemoWindow() application_window.show() this_application.exec_()