psychopy.org | Reference | Downloads | Github

Error in adding free-ranging participant text to online experiment


#1

Hi @jon & community,

Description of the problem: I need to add questions to an experiment where the participant responds by typing text interactively into the browser.

I have incorporated the solution provided by: https://github.com/jacanterbury/PsychoPy-snippets

The solution works in PsychoPy builder, but I get the following error when I try the run the same experiment module online in Pavlovia:

Unfortunately we encountered an error:
ReferenceError: Can’t find variable: inputText**
Try to run the experiment again. If the error persists, contact the experimenter.

I haven’t been able to find threads that address this issue, so any thoughts on how to fix this error would be greatly appreciated!

Best,
Mira


#2

I am not sure that example will work online, unless you have modified it to run on JavaScript. Here is an example of how you would get typed text to appear on screen online. Assuming you have a text component called “text”, and a code component - in “Every Frame”:

let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) {  // at least one key was pressed
  textAdd = theseKeys[theseKeys.length-1]; 
  }

// check for quit:
if (theseKeys.indexOf('escape') > -1) {
    psychoJS.experiment.experimentEnded = true;
}
if (textAdd) {
    text.setText(text.text + textAdd);  
    textAdd = undefined;
}


#3

Hi @dvbridges
Thank you so much for the code. It doesn’t seem to run, unfortunately. Am I to enter the entire code under ‘begin experiment’?

I have taken a screenshot of the code and text component, and my loop file, to help find what I have done incorrectly.

Thanks again for your help. I am grateful.

Mira



#4

Yes, I see why that may be ambiguous. You need to add that code to the Every Frame tab of the code component. Leave your text box in the text component blank, and updates left as constant.


#5

Thank you. I made those changes, but it’s still not running. The screen is just blank and the questions don’t show. I also tried to type on the screen, and it doesnt allow me.

Is there anything else I can try?


#6

Ok, well this works for me:
https://pavlovia.org/run/dvbridges/inputtext/html/

Have a look at the demo attached, and see how your version differs.

inputText.psyexp (6.1 KB)


#7

Sorry to trouble you again @dvbridges, the link works. I can type and see it on screen.

However, when I try to upload the experiment into my pavlovia account, to recreate the link, it gives me the following error:

  • when setting the wrap width of TextStim: text
  • no default wrap width for unit: undefined

Also, is there a way for the code not to type the word ‘space’ when the participant uses a space bar in writing their sentences? or type the word ‘return’ when participant presses enter to move to the second question?

And, I am unsure as to where to upload the two questions. I assumed they can be uploaded in the loop. But am I correct to assume that I cant leave the text box in the text component blank as you suggested?

Mira


#8

@wheights01, what version of PsychoPy are you using?


#9

PsychoPy 3.0.5


#10

Are you attempting to upload the example I gave you?

Yes, you have to check for whether or not a space, or return has been pressed in the code, as we have with the escape key. If those keys have been pressed, do not update the text in the text component.

It depends on how you want the questions presented. If you want them presented sequentially on every trial, the the easiest option will be to have two routines in which you add your question text from your conditions file to a separate text components. For this, you need to change how the conditions file is formatted, so rather than having two rows in a column called word, you have two columns, each holding 1 question, called question1 and question2. In the first routine you may have a text component called “q1” and in the text box, set to update on every repeat, you would add $question1. In the second routine with the second text component you would have the same with $question2.

Instead of having two routine,s you could have a nested loop for your two questions, but I think the two routines is clearer to read for you in Builder.


#11

Thats right, I tried to upload your example first, just to catch errors that I often make in this process (a way to trace my errors).

I have made the changes to the routine/condition file as you suggested with question1 and question2 (thank you!).

I tried to write JS codes per your suggestion. Using your notes as a template, I was hoping to get these in:

escape = end experiment
return = move to the next routine/trial
space = print space character, e.g. " "
backspace = remove last character
left/right shift = shift character

In builder, the experiment doesn’t run, it shows question1 and I can’t type anything on screen. It is still. And when I tried to upload on Pavlovia, I got the same error:

when setting the wrap width of TextStim: text
no default wrap width for unit: undefined

Below is the code that I wrote (which is undoubtedly wrong :slight_smile: ). I have learned a lot with Psychopy (and its community), but at my heart, I’m a psychologist and not a programmer, so any thoughts or leads would be greatly appreciated!
Mira

Under ‘Each Frame’
let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) { // at least one key was pressed
textAdd = theseKeys[theseKeys.length-1];
}

// check for quit:
if (theseKeys.indexOf(‘escape’) > -1) {
psychoJS.experiment.experimentEnded = true;
}
// check for return (move to next frame):
if (theseKeys.indexOf(‘return’) > -1) {
psychoJS.experiment.routineEnded = true;
}
if (textAdd) {
if (theseKeys.indexOf(‘space’) > -1) {
text.setText(text.text + " ");
textAdd = undefined;
} else if (theseKeys.indexOf(‘backspace’) > -1) {
text.setText(text.text + textAdd[textAdd.length-1]);
textAdd = undefined;
} else if (theseKeys.indexOf(‘shift’) > -1) {
//how to shift a character?
textAdd = undefined;
} else {
text.setText(text.text + textAdd);
textAdd = undefined;
}
}

:


#12

Would you mind wrapping your code in backticks (use 3 backticks above and below code), to make it readable? They look like this : `, not to be confused with apostrophe: ’

I do not know why you are getting this error and I am not. Which version of PsychoPy are you using? Also, just try actually setting a wrap width - set it to something sensible and give it another go.


#13

Hi,
Sorry I didn’t know about the three backticks. Please see below. I’m using PsychoPy 3.0.5
As far as wrap width, is 500 appropriate?

Thanks again for all your help.

if (theseKeys.length > 0) {  // at least one key was pressed
  textAdd = theseKeys[theseKeys.length-1]; 
  }

// check for quit:
if (theseKeys.indexOf('escape') > -1) {
    psychoJS.experiment.experimentEnded = true;
}
// check for return (move to next frame):
if (theseKeys.indexOf('return') > -1) {
    psychoJS.experiment.routineEnded = true;
}
if (textAdd) {
    if (theseKeys.indexOf('space') > -1) {
        text.setText(text.text + " ");  
        textAdd = undefined;
    } else if (theseKeys.indexOf('backspace') > -1) {
        text.setText(text.text + textAdd[textAdd.length-1]);  
        textAdd = undefined;  
    } else if (theseKeys.indexOf('shift') > -1) {
        //how to shift a character?  
        textAdd = undefined;  
    } else {
        text.setText(text.text + textAdd);  
        textAdd = undefined;
    }
}

#14

Can I see a screenshot of the advanced tab please?


#15

Yes, please see below:


#16

Ok, if you have set your screen units to pixels in experiment settings, then that wrap width will make sense. Give it a try and let me know.


#17

I am now getting a new error David.

  • when converting a position to pixel units
  • unknown position units: undefined

Any thoughts about this one? Thanks again.

Also, just tried running it in builder, and the screen is still, with the first question on the screen, not letting me type anything.


#18

Apologies, I did not mean for your to change your screen units to pixels. That was the only logical choice given your value for wrap width. The screen is not showing anything up because your text component is not using pixel units, more like norm or height units, as your text size is .1, which makes sense in norm/height units, but not in pixel units, because .1 of a pixel will not show up on screen - it is too small.

Have a read of the following link

https://www.psychopy.org/general/units.html#units-for-the-window-and-stimuli

I would advise that you set your preferred screen units to “height” for online studies. If you are still getting errors after this, please share your psyexp file with me so I can see what is happening.


#19

Ahh I see, thank you for explaining that David. I set the screen unit to ‘height’ (under experimental settings), and left the general unit to norm (under preferences). I am not sure if this was creating errors but under experimental settings, the PsychoPy version was not 3.0.5 (even though I only have PsychoPy 3.0.5 and deleted all other versions before updating). I reset it to 3.0.5.

I am getting an error directing me to the JS syntax for the input text (line 740). Sorry to trouble you with this again, but I’ll take you up on your generous offer (and send you my psyexp file).

Thank you so much David!

54321_metadata.csv (10.7 KB)
andersen302.psyexp (286.2 KB)
Consent_Turk.xlsx (9.8 KB)
Debrief_Turk.xlsx (9.7 KB)
Distractors.xlsx (24.8 KB)
Evaluation.xlsx (8.0 KB)

textInputTest.xlsx (8.5 KB)


#20

Hi @dvbridges
Just wanted to touch base with you again to see if you may have had time to take a look at my JS code. Thanks so much!
Mira