Adding an inline code to the builder causes problem with registering keypresses in excel file

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

In my experiment, I have to show a polygon after a key is pressed, I am using this code for that:

kb = keyboard.Keyboard()
keys = kb.getKeys(['z', 'm'], waitRelease = False, clear=True)


if 'z' in keys:
    core.wait(1)
    left_polygon.opacity = 1
    
    
elif 'm' in keys:
    core.wait(1)
    right_polygon.opacity = 1

But, the problem is, the experiment is not recording any keypress, how can I solve that?

Found out that this code interferes with Psychopy’s internal working, so I changed the code to this:

responses = event.getKeys()

if 'z' in responses:
    core.wait(1)
    l_r.opacity = 1

    
elif 'm' in responses:
    core. Wait(1)
    r_r.opacity = 1

I further want to show a feedback in case no key is pressed, else move on to the next routine if it pressed. For that, I created a text stim to show the feedback and added this code below the above one:

if 'z' in responses:
        core. Wait(1)
        continueRoutine = False
        
    elif 'm' in responses:
        core. Wait(1)
        continueRoutine = False

but this code interfering with the previous one.

Hello,

what do you mean with

Why do not combine everything in one if-else construction?

if 'z' in responses:
    core.wait(1)
    l_r.opacity = 1
    continueRoutine = False
elif 'm' in responses:
    core. Wait(1)
    r_r.opacity = 1
    continueRoutine = False

Best wishes Jens

core.wait is not recommended in Builder. For static feedback I sometimes made a copy of the trial routine and remove the interactive elements.

this code will proceed to next trial immediately after showing the rectangle, I need the elements to be shown for exactly 7 seconds before moving on to the next trial.

that is a possible solution, but I need to show the feedback for exactly 7 seconds and in case no keypress was made, I have to show a message for exactly 2 seconds, how can I achieve that?

Hello shail,

well, create a routine for your feedback-message, set the duration of a variable, add an if-construction in the begin tab of that routine and check whether a key has been pressed. If yes, set the duration-variable of your feedback to seven seconds, if no set the duration-variable of your feedback to two seconds.

Best wishes Jens

1 Like

yea, I thought about this from both of your and @wakecarter’s comments, let’s see if that works, I guess this is the only possible solution for this question right now.

How do I change the duration of a component?

Hello shail,

You set the duration in the respective field duration of your component.

grafik

But this is a very basic question. So, you might want to read Resources for teaching and learning — Workshops for PsychoPy 2022 2022 first.

Best wishes Jens

Not that, I want to change the duration of a component dynamically with code.
So, here is the code that I am trying, what I want is, I want to show a feedback message “Too Late”. The duration for this message must be 1 second if no key was pressed, else 0 seconds (strictly, I don’t want to show a blank screen with opacity 0), here is the code that I am trying:

responses = event.getKeys()

if len(responses) != 0:
    if 'z' in responses:
        feedback_duration = 0
        core. Wait(1)
        l_r.opacity = 1

    elif 'm' in responses:
        feedback_duration = 0
        core. Wait(1)
        r_r.opacity = 1
else:
    feedback_duration = 1

But, it didn’t work. So, I tried debugging, so I tried this code, and the output (testt) was printed on a text component:

responses = event.getKeys()

if len(responses) != 0:
    if 'z' in responses:
        testt = "Something"
        core.wait(1)
        l_r.opacity = 1

    elif 'm' in responses:
        testt = "Something Else"
        core.wait(1)
        r_r.opacity = 1
elif len(responses) == 0:
    testt = "Nothing"

the problem with this code is, if I remove this code:

elif len(responses) == 0:
    testt = "Nothing"

I get the correct output, i.e. “Something” is printed when I press ‘z’ and ‘Something Else’ is printed when I press ‘m’, but as soon as I include the code that is mentioned above, “Nothing” is printed for all cases.

If you want the duration of a routine to be 1 or 0 then set it to 1 second and put a conditional continueRoutine = False in the Begin Routine tab of a code component

1 Like

Hello shail,

it is better to describe with words what you intend to do than to past code that does not work. :wink: Apparently you only want to show feedback when your participant did not respond.

try this

if not key_resp.keys:
    msg="Failed to respond"
    msgDuration = 1
else:
   msgDuration = 0

Set the duration field of your component to $msgDuration

1 Like

Ok, I figured it out:

added this line in the begin routine tab:

feedback_duration = 1

and in every frame tab, added this code:

responses = event.getKeys()

if len(responses) != 0:
    if 'z' in responses:
        feedback_duration = 0
        core. Wait(1)
        l_r.opacity = 1

    elif 'm' in responses:
        feedback_duration = 0
        core. Wait(1)
        r_r.opacity = 1

and it worked!
will keep you posted after properly checking everything.

Thank you, and sorry for not explaining properly, I will try this one too.

Hello shail,

still get rid of the code you don’t need, e.g. core.wait() aso.

Best wishes Jens

Actually, I need core. Wait() I need to show the polygon component after 1 second of the keypress.

Hello

from the crib sheet:
Just don’t (in Python or JavaScript) if you are using Builder. There is an implicit win.flip() at the end of the Each Frame tab and additional flips or halting the process with waitKeys or while will confuse the Builder code.

Best wishes Jens

1 Like