User Tools

Site Tools


PyGame Mouse Events

In class, we looked at capturing mouse events with PyGame and then using those to interact with our OpenGL environment. Recall that one of the steps in our display loop is checking for user events. The contents of our check_events() function from our previous examples looked like the following:

    def check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:

This is where we will need to add another if-statement to detect that a mouse button has been pressed.

            if event.type == pygame.MOUSEBUTTONDOWN:

This code will detect if any mouse button has been pressed, not just left clicks or right clicks. To figure out which button has been pressed, you'll need to poll PyGame and get a list of button states. PyGame gives you a list of 1s and 0s each of which represents one of your mouse buttons. The list is three elements long and looks something like this:

[ 0, 0, 0 ]  -->  No buttons pressed
[ 1, 0, 0 ]  -->  Left button pressed
[ 0, 1, 0 ]  -->  Middle button pressed
[ 0, 0, 1 ]  -->  Right button pressed

The first element of the list is the state of the left button, the second element is the state of the middle button, and the third element is the right button. It is important to note that some mice have several more buttons. It has been my experience that these buttons do trigger mouse click events, but do not show up in this list of button states. To get this list and check the buttons, you need to add the following code to your event checking if-statement from above.

                    if button[0]==1:
                        print "The left button was pressed!"
                    elif button[1]==1:
                        print "The middle button was pressed!"
                    elif button[2]==1:
                        print "The right button was pressed!"

Catching a mouse button press is important, but you usually also want to know where on the screen the button was pressed. This is sometimes referred to as the click location. This click location will be provided in image coordinates (upper, left origin) instead of OpenGL coordinates (screen-centered origin). This means that we will need to convert our mouse click location to something usable by our OpenGL code in order for it to be useful. Below is a quick example of how to do this:

                    pos=list(pygame.mouse.get_pos()) #gets the mouse click location and converts it to a list
		    pos[1]=self.screen.get_height()-pos[1] #converts the upper,left origin to a lower,left origin
                    pos[0]=pos[0]/self.screen.get_width() #dividing by the screen dimensions *normalizes*
                    pos[1]=pos[1]/self.screen.get_height() #your click coordinates to be between 0.0 and 1.0
                    pos[0]=pos[0]-0.5 #adjusts the origin to the center of the screen
                    pos[1]=pos[1]-0.5 #with +/-0.5 along each axis, but we need +/-1.0
                    pos[0]=pos[0]*2.0 #adjusts our +/-0.5 screen range to +/-1.0
                    pos[1]=pos[1]*2.0 #now our click coordinates match our OpenGL screen coordinates
tutorials/ffopengl_mouse.txt · Last modified: 2021/08/01 00:47 by jones