| Reference | Downloads | Github

Translate .keys to javascript

I am trying to translate the the following code in python to javascript:

if sound_1.status == FINISHED:

if done_audio.keys == 'space':
    audio_loop.finished = True

This code component is in a routine that allows the user to test their audio by pressing a specific button for as many times as they please, and when they are satisfied with their audio, they press space to continue to the next routine. This code works fine in the builder, but I suspect there is an issue with my javascript translation (trying to put this experiment online).

This is the JS code I have right now:

if sound_1.status == FINISHED {

if done_audio.keys == 'space' {
    audio_loop.finished = true;

Is there any obvious issue? I’m unsure about the .keys function.

Thanks in advance.


if “space” in done_audio.keys:

which should work in Python and translate correctly

The python code works as I have it. Do you mean replace the javascript version if statement with “if ‘space’ in done_audio.keys {” ?

My approach to putting PsychoPy online is to recode my Python as much as possible so that it works both offline and online when auto translated.

I have a crib sheet with my notes on how to get things working using this approach:

For keyboard responses I now often use Python code like:

keys = event.getKeys()
if len(keys):
    if 'escape' in keys:
    elif 'return' in keys and (captured_string=='0' or len(captured_string) ==2):
    elif len(captured_string) == 2:
    elif '0' in keys:
        captured_string = captured_string+'0'
    elif '1' in keys:
        captured_string = captured_string+'1'
    elif '2' in keys:
        captured_string = captured_string+'2'

The JS auto translation looks very different, so I’ve just checked the auto translation for if ‘space’ in done_audio.keys and it comes out as:

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 = {};

if (_pj.in_es6("space", done_audio.keys)) {