Variable name in "allowed keys" field of keyboard component causing error

Version: PsychoPy 2021.1.2

When I try to put the name of a variable in the “allowed keys” field of the keyboard component in builder I get the error message below, saying that the name of the variable is not defined and “allowed keys list is invalid”.

The variable is defined in a “begin experiment” tab of a code component like so: keys1 = [‘1’,‘2’,‘3’] I have also tried to give a variable defined in an excel sheet. Same error.

I think I am doing everything correctly with the code and interface components, so I’m guessing it is a problem with builder?

Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\components\keyboard_init_.py”, line 238, in writeFrameCode
keyList = eval(allowedKeys)
File “”, line 1, in
NameError: name ‘keys1’ is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\builder\builder.py”, line 1165, in runFile
self.app.runner.panel.runLocal(event)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\runner\runner.py”, line 635, in runLocal
exp=self.loadExperiment())
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 73, in generateScript
compileScript(infile=exp, version=None, outfile=filename)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 242, in compileScript
_makeTarget(thisExp, outfile, targetOutput)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\psyexpCompile.py”, line 221, in makeTarget
script = thisExp.writeScript(outfile, target=targetOutput)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment_experiment.py”, line 212, in writeScript
self_copy.flow.writeBody(script)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\flow.py”, line 217, in writeBody
entry.writeMainCode(script)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\routine.py”, line 206, in writeMainCode
event.writeFrameCode(buff)
File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\components\keyboard_init
.py", line 241, in writeFrameCode
self.params[“name”], “Allowed keys list is invalid.”)
psychopy.experiment.utils.CodeGenerationException: key_resp_7:

Yes, it seems there is a bug in Builder with generating the script in this situation.

@TParsons – this looks like one for you? I’m sure this has been an issue that has arisen (and been squashed) before, but seems to have recurred recently (was present at least in 2021.1, and still occurs in 2021.1.3).

Ah I see the problem - because allowedKeys needs to be a list, it gets wrapped in [] at one point in code generation. Previously this was done manually in the Keyboard class, now it’s handled by the new param types system, so happens at an earlier state. So when Keyboard checks whether allowedKeys is a valid variable name, it doesn’t consider that it might already be wrapped in []. Will get a fix in for the next release!

Very nice to have that cleared up. Thank you.

So will my program work with an older version where this problem is not present? And will it work if I run it online?

You’re probably best off waiting until the next release rather than trying to alter a 2021.1 experiment to run in 2020.2 - but it should work online! The problem is with compiling to Python, JavaScript uses totally different methods.

Hi, we have this problem too. We are running 2021.1.3 in Builder view i.e. the latest release.
We are unable to specify the variable “corrAns” as a variable for Allowed keys within key_resp.
Will you be releasing 2021.2 in the very near future? i.e. within a week or so?

The next version will be 2021.1.4 (2021.2 will be the major release in summer!) but yes hopefully soon, the fix for this is in as a Pull Request on GitHub:

We were hoping to release sooner but due to a fire at the datacenter where Pavlovia is hosted we’ve not had the time to review these and compile a new release.