Playing sound when key is pressed on Pavlovia

URL of experiment: Pavlovia

Hey PsychoPy-Experts,

I would simply like to play a sound when a specific key is pressed without ending the routine. I added a code component and it is working locally but when uploaded to Pavlovia the site gets stuck on “Initialising the experiment” and the JS console shows this error: “Uncaught SyntaxError: Unexpected token ‘{’” in line 117 of the JS-script.
I read in another post that I might have to edit the JS-script but honestly I have no clue what to change. Can anyone help?

I’m also open to use another way to be able to play a sound on a key press if anyone has an idea that is working on Pavlovia.

This is the auto converted code in my code component:

Begin experiment:

import {prefs} from 'psychopy';
import {sound} from 'psychopy';
var c;
prefs.hardware["audioLib"] = ["pyo"];
c = new sound.Sound("zapsplat_bells_bell_small_hand_ring_ping_single_soft_001_67990.ogg");

Each Frame:

var _pj;
function _pj_snippets(container) {
    function in_es6(left, right) {
        if (((right instanceof Array) || ((typeof right) === "string"))) {
            return (right.indexOf(left) > (- 1));
        } else {
            if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
                return right.has(left);
            } else {
                return (left in right);
    container["in_es6"] = in_es6;
    return container;
_pj = {};
keys = psychoJS.eventManager.getKeys();
if (keys) {
    if (_pj.in_es6("r", keys)) {;

Hi @Kathrin,

the first thing that you can try is deleting these lines.

They are all PsychoPy-specific and interfere with the JS code.

Maybe that’s already sufficient to make it work, otherwise, you could try simplifying the “Each frame” code to

keys = psychoJS.eventManager.getKeys();
if (keys) {
    if (keys.includes("r") {;

Let me know if any of that helps!

Hi @ajus

Thank you so much for your answer!
I changed both code snippets the way you suggested (I added a bracket in the if condition that was missing I think). Pavlovia is now able to initialise the experiment but I now get a different error:

And the browser console shows this:

Does anyone understand what it is trying to tell me? :smiley: I am a bit overwhelmed.


I don’t know about this error, but I got a little example to do what you want. The only thing that I changed is the definition of the sound to

c = new sound.Sound({
    win: psychoJS.window,
    value: 'A',
    secs: (- 1),

(“A” only because I don’t have your ogg file)

The “Each frame” code remained

keys = psychoJS.eventManager.getKeys();
if (keys) {
    if (keys.includes("r")){;

Have a look. I hope it helps you to get it running. I am using PsychoPy version 2022.2.4

keysound.psyexp (7.7 KB)

By the way: The error might be totally unrelated to the one you originally posted about and simply the next one that pops up when your script runs. According to the console output, you could look to line 167 of your script for the error.

Thank you so much for your quick support! It’s working now :raised_hands:

1 Like