psychopy.org | Reference | Downloads | Github

Using mouse.getPressed()[0] == True online

I’ve just figured out a solution an issue I was having and thought I’d post here to see if I can get a clearer understanding of what’s going on

The following Python code wasn’t working as expected online

mouse.getPressed()[0] == True

The JavaScript autotranslation was using triple equals

mouse.getPressed()[0] === true)

Changing the JavaScript translation to double equals (==) appears to have solved the issue. My understanding is that mouse.getPressed()[0] should return a boolean - is there a reason that JavaScript shouldn’t return true with strict type checking?

Hi @andrewildman, if you take a quick look at the online docs, you see that getPressed returns a list of buttons that are currently pressed. The JavaScript === comparison operator checks for equality of both value and type, whereas == only compares values. In your code, if you are compare an int to a boolean value using ===, it returns false, this is why your initial translated code failed. Instead you could use the following to fix the issue without needing to alter the JS:

mouse.getPressed()[0] == 1
1 Like