How to get space bar to end routine only after rating is collected

OS : Trisquel 8 (based off of Ubuntu 16.04 LTS)

Python version: Python 3.5.2

PsychoPy version : PsychoPy 3.0.7.

Standard Standalone? (y/n) : y

I am trying to figure out how to control the sequence of mouse clicks and key presses in the ratings of sound stimuli. I want a participant to listen to a sound, rate the sound using a numerical (1 – 4) scale with a mouse click, specifically the “Slider” (with the red triangle), and then press the space bar to confirm the rating and continue the routine. However, I don’t want the participant to be able to continue to the next routine with the space bar until after the rating has been recorded with the mouse click.

I could not figure out any way of doing this without a code component. So, I adjusted a code component I found on a psychopy forum (Recording a correct answer from a rating scale) and included it in my ratings routine in the every frame tab:

http://dpaste.com/06QVQ59

This code does the job offline, but not when I run my experiment online. The last time I checked there is no automatic conversion of the python code components to javascript. Is this still the case? I’m having difficulties getting help with converting this code as I myself don’t know javascript.

Is there any way of doing this in the Builder without a code component?

Hi @ccart, you just need to convert the code to JavaScript and put it in the JavaScript panel in the code component (see code type menu in code component):

Try the following:

let theseKeys = psychoJS.eventManager.getKeys({keyList:['space']});
   
// check for quit:
if (theseKeys.indexOf('escape') > -1) {
  psychoJS.experiment.experimentEnded = true;
}

if (theseKeys.length > 0) { // at least one key was pressed
  let completed_ratings = 0;
  for(Slider of [mainSoundsratings]) {
    if (slider.getRating() !== 'undefined') {
      completed_ratings =  completed_ratings + 1;
    }
  
  if (completed_ratings === 1) {
        continueRoutine = false; // end now
  }

Hi @dvbridges, thanks for your help. I put your JavaScript code in the JavaScript panel of the code component. I then synced the experiment and attempted to run it online. However, all that loaded was a blank screen and the text “loading the experiment.”

The only thing showing up in the output of the coder view is “Failed to parse as JS by esprima.”

The chrome debugging console says:

Tone.min.js:The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. Autoplay Policy Changes  |  Web  |  Google Developers
t.Context @ Tone.min.js:7

  • Tone.js r12 *
    Uncaught SyntaxError: Unexpected end of input /run/cc954/test2pyschopypublic/html/test2PyschoPyPublic.js:402

This is a pic of the message:

loadingexperiment_debugmesg

Strangely, even after I removed the JS code component, and kept the Python code component I had before, the same “loading the experiment” screen pops up with the same online debug message. So, now I’m getting this error regardless of whether the JS code component is there or not, but it only started after I synced with the JS component. I believe in the past there have been times in which I sync from the builder, but it does not actually sync and still tries to run a previous version online. Not sure if that’s what is going on here.

Let me know if there is a more debugging info I could provide that would help solve the issue. Thanks.

carla

Apologies, I missed off some curly braces to close the code blocks:

let theseKeys = psychoJS.eventManager.getKeys({keyList:['space']});
   
// check for quit:
if (theseKeys.indexOf('escape') > -1) {
  psychoJS.experiment.experimentEnded = true;
}

if (theseKeys.length > 0) { // at least one key was pressed
  let completed_ratings = 0;
  for(Slider of [mainSoundsratings]) {
    if (slider.getRating() !== 'undefined') {
      completed_ratings =  completed_ratings + 1;
    }
  
  if (completed_ratings === 1) {
        continueRoutine = false; // end now
    }
  }
}
1 Like

Thanks @dvbridges, I think we are getting closer. Now, the experiment does load online, but as soon as I hit the space bar in the rating screen to move to the next routine, I get this error window:

Here is the debug message from the chrome console:

I looked at the PsychoJS documentation for Slider, but, unfortunately, I myself can’t figure out how it should be properly defined. Do you have any suggestions? Thanks

Ah, yes you have to define the temporary Slider variable in the loop. Apologies, I translated this code without testing it, so it will need a bit of debugging. Maybe we should rename that variable to avoid confusion with the actual Slider class. Try:

let theseKeys = psychoJS.eventManager.getKeys({keyList:['space']});
   
// check for quit:
if (theseKeys.indexOf('escape') > -1) {
  psychoJS.experiment.experimentEnded = true;
}

if (theseKeys.length > 0) { // at least one key was pressed
  let completed_ratings = 0;
  for(var thisSlider of [mainSoundsratings]) {
    if (thisSlider.getRating() !== 'undefined') {
      completed_ratings = completed_ratings + 1;
    }
  }
  
  if (completed_ratings === 1) {
        continueRoutine = false; // end now
    }
}

Hi @dvbridges, Thanks for the quick reply. Now, I can continue the routine by pressing the space bar. The only problem is that pressing the space bar allows me to continue the routine before any rating is collected with the mouse click.

The data output in the csv file records the ratings with the mouse click, so it is not an issue that the mouse click is not recorded at all. Maybe this has to do with the completed_ratings variable?

This was a problem with the slider rating equality check with ‘undefined’. Turns out getRating() returns an actual undefined type, rather than a string of ‘undefined’. So change:

if (thisSlider.getRating() !== undefined) { // so equality check is with an undefined type, not string

Here is a working example if that fails sliderCode.psyexp (6.2 KB)

1 Like

Yes, now it works! Thank you for clarifying undefined as a type as opposed to a string. The earlier errors make sense now. Thank you for all your help!

1 Like