| Reference | Downloads | Github

Trouble translating PsychoPy libraries to JS (Mouse + Sound)

Howdy! I’ve been migrating a project I made in builder over to Pavlovia and got stuck on some of the libraries. I looked through the other posts on here and docs online but couldn’t figure out what I was missing. Thanks for any help!

The original Python:

import psychtoolbox as ptb
from psychopy import sound,event

mouse0 =event.Mouse(win=win)

I’m just trying to translate the event.Mouse(...) over to JS, but nothing I do seems to work. The automatic translation is mouse0 = new psychoJS.eventManager.Mouse(...);, but this throws an error when run (not a constructor).

Then I tried mouse0 = new Mouse(...); per the docs here but was given the error that Mouse was not defined. As a last ditch I tried mouse0 = new core.Mouse(...); which didn’t turn any errors, but returns a null object that I can’t use .isPressedIn() with.

Additionally, I’m trying to use the Sound constructor to play a sound file. The original Python looks like this:


I tried a few similar methods and couldn’t get it to run either; I’m assuming I’m doing the same thing wrong here too.

For additional context, I don’t have it importing any JS libraries; psychtoolbox and psychopy are both Python-specific, and I couldn’t find a reference as to what libraries I should be specifying within the JS. This might be what I’m doing wrong.

How do I instantiate these objects? Is there a good place to find more extensive code examples of all these? I appreciate the help!

I’ve found that it is best to use Builder components to create mouse and sound objects.

If you add a mouse component (e.g. called mouse) to your first routine then you can call it (using .isPressedIn etc.) for the whole of your experiment. Set a short duration in that first routine and it won’t interfere.

Sound components can be created with 0 volume before you need them and then use e.g. sound_1.setVolume(1) when you need them.