I want to create an assignment that waits for a key response

Hello.

I am thinking of setting up an experiment in which subjects are asked to press a key on a keyboard and if they press a certain key, they get the correct answer, if they press a different key or if they do not respond, they get the incorrect answer. I am also thinking of setting a time limit for each execution, such as within 2 seconds.
I am trying to write a code that will do both of these things, but it is not working.
How should I do this?
Specifically, I am having trouble with this.
Record the key pressed within the time limit and the reaction time.
If there is no response, specify it with an if statement.

Thank you in advance for your help.

Hi @OnoreCoffee,

Have you considered using the Builder interface to achieve this? A lot of the things you’re looking to do are stored automatically by Builder.

Thanks,

Kim

Thank you for your reply.
I would like to use coder because I would like to make the conditional branches more detailed and send triggers to EEG.
I was going to try this in Builder, but it appears to be difficult to configure it to wait for a key response for a certain period of time, and then display the image we have set up if the response is different from what we specified.
Also, it appears that the auto-generated code for this cannot be used elsewhere.
Thank you in advance.

        stopwatch.reset()
        while stopwatch.getTime() <= 2.0:
             stimuls_sA1.draw() #left
             stimuls_sA2.draw() #right
             win.flip()
             key1 = event.getKeys(timeStamped = stopwatch)
        if key1 == "left":
             collect.draw()
             win.flip()
             core.wait(2.0)
        elif key1 == "right":
             collect.draw()
             win.flip()
             core.wait(2.0)
        else:  # No response was made:
             error.draw()
             win.flip()
             core.wait(2.0)

If you’re planning on sending triggers to an EEG acquisition device I’d certainly recommend using Builder. core.wait() isn’t the best way to control timing, you can read more about that here.

You can still use all of your conditional statements using a code component (found in the Custom component drop down), they can be as detailed as you like. I’ve made a very basic example of how to do this, hopefully you can work from there?
key_demo.zip (10.0 KB)

Thanks,

Kim