| Reference | Downloads | Github

Code component for typing response

Hi, everyone!
I am struggling with translating python code to JS.
In my experiment, participants have to solve anagrams and type the answers. The experiment is made in Builder, and I have a code component that allows participants to type their answers. Here it is:

# Begin routine
corr = 0
import pyglet
from pyglet.window import key

def on_text(text):
    global mytext
def on_key_press(symbol,modifiers):
    global continueRoutine,mytext
    if symbol == key.ENTER:
        if mytext == corans:
            corr = 1
            corr = 0
    elif symbol == key.BACKSPACE:

wins = pyglet.window.get_platform().get_default_display().get_windows()

for window in wins: 

# End routine
for window in wins: 

As I know, code components have to be translated to JS manually in order to make them work. Unfortunately, I have zero experience in JS, but I made an attempt:

var mytext=''
function on_text(text){

function on_key_press(symbol,modifiers) {
    if (enter>0){
   }    else if (backspace>0){

Surely, it doesn’t work. So, I have two questions:

  1. Is there a convenient way to write a JS code doing similar things?
  2. Could anyone help me rewrite the python code above in JS?
1 Like

Hi there,

If you search on Pavlovia there is a demo called textInput that includes a JS code component which allows participants to type their answers.


1 Like

@jretz Thank you for the answer! I used the code from the demo you mentioned.

1 Like

Hi @jretz,
Would you happen to have any tips on text input?
I have a question that is answered by two buttons and it is showing up in the typed response.
What I meant by that is that the question before the typed response is : “Has the item changed?” answering with q or p and then on the next slide with typed response slot, the typed response blank already has either q or p typed in it.

Hi @peter101

I’m not sure from your description quite what you are after. It sounds like:

  1. Participants are asked ‘Has the item changed?’ to which they answer ‘q’ or ‘p’
  2. They then proceed to the next ‘slide’ where they should write whatever it is the item is in a text entry box that should be blank

If that is correct, it sounds like you are having a problem with the fact that the text input has already picked up the ‘q’ or ‘p’ and put it in your blank entry box, but you don’t want it to be there? It sounds like you need to play around with the order of where you are getting PsychoPy to record the keyboard entry, as it seems to be recording it from the ‘p’ and ‘q’ bit when it sounds like you don’t want it to remember those bits. Are these ‘slides’ as you call them, in the same routine? You could try separating them to have one that has the yes/no response, and a new routine where you record the text entry.

It’s really hard to answer your question without knowing more about the structure of your trial set-up (and I’ve only used the text entry stuff one time so I’m still a beginner in that respect!).

Hope this helps

Hi Jenny!

I believe the issue was that for the type response, it is pulling out the typed response directly from Py temp data, so the response from previous routine (q or p) still lingers around and shows up in the screen of the typed response routine. I solved this issue by resetting the Py temp data by using psychoJS.eventManager.getKeys(’’)

Thanks for the help!