Text input field with PsychoPy 3.1.2

OS: macOS High Sierra 10.13.6
PsychoPy: 3.1.2

I am new to PsychoPy and I have been trying to design a cued recall test for my experiment. In this test, the cue word will be presented to participants with a blank text box next to it, and participants will be asked to type the target word for the presented cue.

I tried to use the prepared script from GitHub - jacanterbury/PsychoPy-snippets: Some PsychoPy example scripts & snippets for easy sharing. And I know that some people reported that it is working, but for me, it gives the following error:

n= len(theseKeys)
TypeError: object of type ‘KeyPress’ has no len()

I also attached the ss of the code I used.

Looking at the previous related topics, I think it has something to do with the version of PsychoPy I am using. Any ideas about how to fix this issue? Any help is highly appreciated!

2

Hi @eylul, I have put together a demo that writes user input on screen, it is similar to your task. It works in native (local) and online PsychoPy. Take a look at the task running at [1], and download a copy for yourself at [2]

[1] https://run.pavlovia.org/demos/textinput/html/
[2] https://gitlab.pavlovia.org/demos/textinput

Hi @dvbridges,

This is exactly what I needed. Thank you so much!!

Hi @dvbridges, I have the same problem as eylul. I tried to use this code

keys = event.getKeys()
        if len(keys):
            if 'space' in keys:
                text.text = text.text + ' '
            elif 'backspace' in keys:
                text.text = text.text[:-1]
            elif 'lshift' in keys or 'rshift' in keys:
                modify = True
            elif 'return' in keys:
                continueRoutine = False
            else:
                if modify:
                    text.text = text.text + keys[0].upper()
                    modify = False
                else:
                    text.text = text.text + keys[0]

but got an error
if modify:
NameError: name ‘modify’ is not defined

Do you know how to fix this? Thank you!

Hi @Xiaotong, try defining modify = False in the “Begin Routine” tab.

@dvbridges Thank you so much, it works! I have another question. This code does not work in the new version either

if response.keys != '1':
    continueRoutine = False

Do you know how to fix this? Many thanks!

Problem solve! This code was in “begin routine” tab before (it works perfectly in V3.0.2). Putting it in “each frame” tab works for v3.2.4.

Thank you, @dvbridges ! I couldn’t make my code work online but this one works perfectly.
One question: is there any chance to allow input in Cyrillic using this code?

Solved it here: Accents in Text Input

Hi @dvbridges

The program (textinput demo) aborts when “backspace” is pressed using some blowers like Firefox. But it works fine with chrome when trying to edit the typed word. Any idea why? I have the same issue with my own experiment when adapting code from the demo.

Looking forward to your reply.
Miao

Hey David, thanks so much for giving us a demo to work from. I followed your link https://run.pavlovia.org/demos/textinput/html/ which worked, thus demonstrating that the javascript version of your code can work.

It also works when I download the intputText.psyexp file from github and run it locally from psychopy.

However, if I try to sync the inputtext.psyexp file to Pavlovia and then run it there, I get a “Unspecified JavaScript error” when I try to run it.

My guess is that you have made some modifications directly to the .js code that you have not yet already made to the “code” component in the “trial” routine of the inputtext.psyexp file.

This is a problem because I want to integrate your free text input routine into an existing experiment so that I can get participant feedback.

  1. Am I correct in my guess of why it runs when I follow your HTML link but not when I download the .psyexp file and try to manually run that in Pavlovia

  2. Can you provide an updated inputtext.psyexp file whose code component contains the javascript necessary for it to run online?

@atryan , I could provide another example, but have you tried using textbox2 in the latest PsychoPy? Its an editable textbox that is compatible with Pavlovia - here is a demo

https://run.pavlovia.org/lpxrh6/textboxdemo/

@dvbridges , thanks for your post. I have indeed tried using textbox2 in the latest PsychoPy release (2.2.10). While I could get texbox2 to work in Pavlovia when I created a new experiment from scratch with just the textbox (and a continue button) in it, every time I tried to add it to my existing experiment, it throws a “TypeError: state.toLowerCase is not a function” error every time I try to run it on Pavlovia.

I tried everything I could think of, including:

  1. It runs in the offline version
  2. Tried a different browser and new computer
  3. I thought maybe since I originally created the experiment in an earlier version of psychopy, that maybe Pyschopy Builder was inserting the old non-Pavlovia compatible version of textbox. So I (a) deleted and reinserted the textbox in the experiment, (b) used a different computer with a fresh install of the latest psychopy stand alone, (c) copied the experiment files into a new folder, started a new Pavlovia project, (d) went into the experiment settings and manually selected Psychopy version 2020.2.10.
  4. I tried deselecting the editable option
  5. I tried putting placeholder text in the box

No matter what I did, Pavlovia threw the “state.toLowerCase is not a function” error. The only way I could make it stop is by disabling or deleting the textbox, which then caused the experiment to run fine.

Any thoughts on what might be causing this? I’ve included the output from the Firefox developer console when I ran the experiment below.

Here’s a link to the experiment page: https://pavlovia.org/atryan/migt-v1-05

And the repository: https://gitlab.pavlovia.org/atryan/migt-v1-05

And a shared Google Drive folder to download the experiment files directly: https://drive.google.com/drive/folders/1mTqvSxxf_eHz_G4XHxMhwysQmBvMK49Z?usp=sharing

Firefox console error message:

TypeError: state.toLowerCase is not a function
DefaultBoxGenerator https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:1105
_buildBoxCache https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:988
_updateBox https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:747
_update https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:737
set substituteText https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:510
TextInput https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:484
_updateIfNeeded https://run.pavlovia.org/atryan/migt-v1-05/lib/visual-2020.2.js:1358
draw https://run.pavlovia.org/atryan/migt-v1-05/lib/core-2020.2.js:2538
setAutoDraw https://run.pavlovia.org/atryan/migt-v1-05/lib/core-2020.2.js:2524
bug_report_2RoutineEachFrame https://run.pavlovia.org/atryan/migt-v1-05/mIGT V1.05.js:2054
_runNextTasks https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1570
_runNextTasks https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1574
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1527
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
update https://run.pavlovia.org/atryan/migt-v1-05/lib/util-2020.2.js:1534
core-2020.2.js:1846:12
FATAL unknown | {} log4javascript.min.js:1:40074
append https://cdnjs.cloudflare.com/ajax/libs/log4javascript/1.4.9/log4javascript.min.js:1
doAppend https://cdnjs.cloudflare.com/ajax/libs/log4javascript/1.4.9/log4javascript.min.js:1
callAppenders https://cdnjs.cloudflare.com/ajax/libs/log4javascript/1.4.9/log4javascript.min.js:1
log https://cdnjs.cloudflare.com/ajax/libs/log4javascript/1.4.9/log4javascript.min.js:1
fatal https://cdnjs.cloudflare.com/ajax/libs/log4javascript/1.4.9/log4javascript.min.js:1
dialog https://run.pavlovia.org/atryan/migt-v1-05/lib/core-2020.2.js:1157
onerror https://run.pavlovia.org/atryan/migt-v1-05/lib/core-2020.2.js:1847
(Async: OnErrorEventHandlerNonNull)
_captureErrors https://run.pavlovia.org/atryan/migt-v1-05/lib/core-2020.2.js:1844
PsychoJS https://run.pavlovia.org/atryan/migt-v1-05/lib/core-2020.2.js:1535
https://run.pavlovia.org/atryan/migt-v1-05/mIGT V1.05.js:17

Sorry not sure, looks like a bug. Best speak to @Becca to see whether she can help, and check whether there is a known solution.

Thanks anyway @dvbridges and have a good weekend!

Hi There,

Looks like a pixi issue. Specifically the attribute _pixi appears undefined for this textbox object, even though it should read something like _pixi: TextInput {_events: i, _eventsCount: 2, tempDisplayObjectParent: e, transform: t, alpha: 1, … I’m going to flag this one to more expert JS opinion - but the error replicates when I fork your task which is a good start point.

Becca

dvbridges,

I know this was like 3 years ago for your but this answer actually really helped me as well. I was having an issue with my code and checking vs yours helped me solve it!

Thank you so much!