# Code component for typing response

**URL:** https://discourse.psychopy.org/t/code-component-for-typing-response/11972
**Category:** Online experiments
**Tags:** code
**Created:** [April 1, 2020, 11:33am UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972 "2020-04-01T11:33:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![artyamm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/artyamm/32/42659_2.png) [@artyamm](https://discourse.psychopy.org/u/artyamm)
#### Post date: [April 1, 2020, 11:33am UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972/1 "2020-04-01T11:33:13Z")

</div>

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:

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

mytext='';
def on_text(text):
    global mytext
    mytext=mytext+text
def on_key_press(symbol,modifiers):
    global continueRoutine,mytext
    if symbol == key.ENTER:
        exp.addData('answer',mytext)
        exp.addData('answer.rt',trialClock.getTime())
        if mytext == corans:
            corr = 1
        else:
            corr = 0
        exp.addData('answer.corr',corr)
        continueRoutine=False
    elif symbol == key.BACKSPACE:
        mytext=mytext[:-1]

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

for window in wins: 
    window.push_handlers(on_text,on_key_press)

# End routine
for window in wins: 
    window.pop_handlers()

```

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:

```auto
var mytext=''
function on_text(text){
    mytext=mytext+text;
}

function on_key_press(symbol,modifiers) {
    if (enter>0){
        exp.addData('answer',mytext)
        exp.addData('answer.rt',trialClock.getTime())
        continueRoutine=false;
   } else if (backspace>0){
            mytext=mytext[:-1];
   }
}

```

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?  
Thanks!

---

<div class="post-metadata">

### Author: ![jretz](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jretz](https://discourse.psychopy.org/u/jretz)
#### Post date: [April 6, 2020, 2:29pm UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972/2 "2020-04-06T14:29:43Z")

</div>

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.

Jenny

---

<div class="post-metadata">

### Author: ![artyamm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/artyamm/32/42659_2.png) [@artyamm](https://discourse.psychopy.org/u/artyamm)
#### Post date: [April 9, 2020, 3:48pm UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972/3 "2020-04-09T15:48:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![peter101](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@peter101](https://discourse.psychopy.org/u/peter101)
#### Post date: [July 19, 2020, 11:46pm UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972/4 "2020-07-19T23:46:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jretz](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jretz](https://discourse.psychopy.org/u/jretz)
#### Post date: [July 27, 2020, 9:51am UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972/5 "2020-07-27T09:51:38Z")

</div>

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  
Jenny

---

<div class="post-metadata">

### Author: ![peter101](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@peter101](https://discourse.psychopy.org/u/peter101)
#### Post date: [July 27, 2020, 5:30pm UTC](https://discourse.psychopy.org/t/code-component-for-typing-response/11972/6 "2020-07-27T17:30:48Z")

</div>

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!
