Adding instant feedback on key press

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.1.1
**Standard Standalone? (y/n): Y
**What are you trying to achieve?:
So, I am a student and this is the first time using PsychoPy, and I’m also new to python! I have some coding experience, mostly just website building (HTML, CSS and Javascript), but I don’t know any of the Python syntax etc.

We have built an experiment that basically has three routines on a loop. One instruction, one Movie component, and then a question that needs a keyboard response answer when pressing the keys ‘1’, ‘2’ or ‘3’. The question is just currently 1 text component with all the text, and one key_resp with the allowed keys 1, 2 and 3.

Now, the issue is this all needs to be on a timer, all “force end routine” is turned off. It needs to correlate to another instrument, basically. However, it is confusing to the participant as, when they answer the question by pressing a key, they get no type of feedback that their response is registered.

I would preferably want either that the alternative they choose will change colour/be highlighted/any indication of which key they responded with. Alternatively, just a sound signal or changing the colour of all the text would be fine. Just some form of signal that their response is registered.

What did you try to make it work?:
So far, I’ve mostly tried to research python syntax in an attempt to add this in the coder, but I realise that that is taking a bit of water over my head. I am gonna assume that what I need is some form of if-statement under the “run routine Question”, that connects key_resp to some output.

What specifically went wrong when you tried that?:
Since I’m not even sure where in the code to put the statements, I haven’t made any real trials. Any suggestions are welcome!

Cheers, and thank you in advance!

You don’t edit the code manually. Instead, from the “custom” component panel, insert a code component. This has various tabs for you to insert code, which will automatically be run at the correct time (e.g. at the start of the experiment, at the beginning of a routine, or on every frame (i.e. screen refresh) during a routine).

e.g. set things up in the “begin routine” tab:

feedback_given = False
some_text_component.color = 'white' # the default state

put something like this in the “each frame” tab:

# if a response has been made:
if not feedback_given and len(your_keyboard_component_name.keys) > 0:
    # give feedback in any of a number of ways:
    some_text_component.color = 'red'
    feedback_given = True # so we don't keep repeating this step

and in the “end routine” tab, re-set the feedback:

1 Like

This worked wonders, thank you! I had completely missed the custom component, and was compiling the code, editing inside it and then trying to run it.