| Reference | Downloads | Github

Mouse always responds as pressed?

Hi all,

OS (e.g. Win10): Mac OS Sierra 10.12.3
PsychoPy version (e.g. 1.84.x): 1.85.0
Standard Standalone? (y/n) Yes
What are you trying to achieve?: Trying to provide feedback based on a conjunction of space bar presses AND mouse click, but the mouse always records as pressed. In order to get a point, participants must click one of two images (doesn’t matter which) and an experimenter must press the space bar (though this will change depending on condition). All other iterations should not give a point (i.e. no space bar and no mouse, space bar but no mouse, mouse but no space bar). We don’t want to end the trial on click, and we don’t want the participant to get any points for not actually clicking one of the images. The participant will be using a bluetooth mouse and the experiment will be mirrored onto another monitor for them, the experimenter will be in control of the main computer and Psychopy program as well as the keyboard.

What did you try to make it work?: Tried using USB mouse as well as laptop mouse (experiment will be run using bluetooth mouse), same results for both. Ideally the code component should contain something like:

if mouse.getPressed() and key_resp_2.corr:
          msg="+1 Point"
if not key_resp_2.keys:
          msg="0 Points
          msg="0 Points"

I’ve tried both mouse.getPressed and mouse.isPressedIn(image)/mouse.isPressedIn(image_2).

What specifically went wrong when you tried that?: Every iteration I can think of results in always giving one message or another, and often just falling through to the “else” statement. When I looked at mouse clicks separately in a test file, I found that mouse clicks are either always pressed or never pressed, depending on the code. The space bar presses alone work just fine, it’s the stipulation that the mouse must also be clicked that I can’t seem to achieve.

I am new to Psychopy and have limited coding knowledge (if that’s not already evident :wink: ). Any help would be immensely appreciated!


SC_DRT.psyexp (41.3 KB)

mouse.getPressed() returns a list of True/False values for each of the mouse buttons. In Python, when you test a list for being True or False, it doesn’t evaluate the entries within the list (which could be anything), but the list itself. An empty list evaluates to False, and a list containing one or more entries (of any type) evaluates to True.

Therefore if mouse.getPressed(): always evaluates to True because the list always contains entries. So to do a test like this, you need to test the value of an individual entry in the list, like this:

if mouse.getPressed()[0]:, which will evaluate to True if mouse button 0 is pressed, and False if it is isn’t. But in your case, it isn’t enough to test if a button is pressed: you also need some spatial information about whether the mouse is actually being used to click one of your images. So you actually probably want an expression something like this:

# use the actual names of your image components:
if (mouse.isPressedIn(imageComponent1) or mouse.isPressedIn(imageComponent2)) and key_resp_2.corr:
1 Like