
This variable is defined before the main loop together with a third variable: mouseClickedInCircle (initialized to False). This means we will need another state variable, mouseDownCircleIndex (initialized to -1), to keep track in which (if any) circle the mouse button is held down. The user releases the mouse in the same circle.The user holds the mouse down in a circle.If mouse.getPressed() = 0 and mouseIsDown:įirst, we will define a circle being clicked as a sequence of the following events: If mouse.getPressed() = 1 and mouseIsDown = False: When the user now releases the mouse button, PsychoPy will see that the button state is 0, but we can combine that with our state variable to check if the mouse was released: # Check if the mouse is pressed down If the user holds down the mouse button, the state variable mouseIsDown is set to True.

Within the main loop, we now continuously check the state of the left mouse button. To detect a mouse click, we first define a state variable mouseIsDown before the start of the main loop and set it to False. In this example I will always use the left mouse button, so I will use mouse.getPressed() to capture that button. The value of each element can be 0 (button is not pressed) or 1 (button is pressed). This will return an array in which each element represents a mouse button. In PsychoPy, we can capture the state of the mouse buttons by using the mouse.getPressed() function. So a mouse click is defined more precisely as the event where the mouse button is first pressed down and subsequently released.

Most likely you will notice that nothing happens until you release the mouse button. Just try to hold the mouse down when the cursor is over some button and keep it down. But it is a little bit more subtle than that. First of all, how do we define a click event? You could say that this is just the event where a mouse button is pressed at the same moment that it is over a shape. # Detect mouse hoverįigure 1: Mouse hover in left circle Implementing mouse click events Of course, if the mouse is currently not over a circle we need to set the fillColor to gray again. If that is the case, the fillColor of the circle is set to red. It iterates over the circles list and uses the contains function to check if the mouse is currently over the shape.
TOUCHSCREEN REGISTER AS MOUSE CLICKS PSYCHOPY CODE
The code below can be added at the begining of the main loop. We simply pass the mouse as an argument to this function, and PsychoPy will do all the calculations and return True if the mouse falls within the circle. contains() function of the circle that we want to check. But we can also do it in a much simpler way by calling the. We could calculate this manually by calling the mouse.getPos() function and then doing a little bit of math to figure out if this position falls within the circumference of each of the circles. A mouse hover event can be defined as the event where the position of the mouse is within the boundary of an object that we are interested in (in this case, a circle). Two circles Implementing mouse hover eventsĭetecting a hover event is actually not that difficult, so we'll start with that. from psychopy import visual, eventĬircle_1 = visual.Circle(win, radius = 50, pos = )Ĭircle_2 = visual.Circle(win, radius = 50, pos = )įigure 1. On each screen flip it also checks if a key has been pressed, which will cause the program to exit the loop and close the window. By placing the circles in a list, I can simply iterate over that list and write the code that needs to be performed only once.įor now, the main loop of the program only displays the circles. This is because we are going to repeat a lot of code for each of these circles. I have added these two circles to a list. The basic setup of our code will create two circles. In this post I will show how you can use that information to react to 'mouse hover' and 'mouse click' events. The PsychoPy event submodule allows you to get information about the mouse position and which buttons have been pressed. This can easily be done using the event.waitKeys() or event.getKeys() functions.

Most computer based experiments require a participant to press a button.
