Response record

OS (e.g. Win10): MacOS version 12.0.1
PsychoPy version (e.g. 1.84.x): v2022.2.5
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:

Hi I am working with Code component in Builder to record multiple responses. Specifically, I have 5 possible responses by pressing “q”, “w”, “s”, “a” and “x” in keyboard. At the same time, I also record another variable (position_XY) for each response. The codes are as follows:

if keys[0] == 'q':
    position_XY = [-5.5,5]
elif keys[0] == 'w':
    position_XY = [5.5, 5]
elif keys[0] == 's':
    position_XY = [5.5, -5]
elif keys[0] == 'a':
    position_XY = [-5.5, -5]
else:
    position_XY = 'x'

Now my problem is that sometimes the experiment could run successfully. But sometimes it reported an error about the response and exited. And it seems that the error was more likely to appear when response was made very quickly. The error is:

Trying to solve the problem, I have also cleaned the key buffer with code at the end of each trial:

keys = []

But it does not work efficiently and now I am not sure what the problem is? Thanks for your time!

Assuming keys or keys_exp (as in your error message) contains a list of Keypress objects (from getKeys() method), I’d say that the keyboard component you created did not register any keypresses or got cleared before you check for the actual keys.

Can you provide some more context (eg. where is this code placed, when do you check for keys, how is keys initialised, etc)?

ask.zip (4.0 MB)

Hi thank you so much and sorry for the late reply!

I have set the keyboard component only records the last response, so I think “keys” variable would just contain one key response.

Here I attached a demo of the experiment procedure! I would appreciate if you can help me check where is the problem! Thank you!

Hi all,

I have solved the problem!

It seems that sometimes the response was not recorded so the “keys” is “”, and consequetly the index error was reported when running the if loop mentioned above.

I have modified the code as follows and now there is no such error reported.

while keys == []:
    keys = event.getKeys()
    thisExp.addData("keysResponse",keys)
else:
    if keys[0] == 'q':
        position_XY = [-5.5,5]
    elif keys[0] == 'w':
        position_XY = [5.5, 5]
    elif keys[0] == 's':
        position_XY = [5.5, -5]
    elif keys[0] == 'a':
        position_XY = [-5.5, -5]
    else:
        position_XY = 'x'

Hope this helps!
Thanks!