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
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?
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.
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.
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
it is better to describe with words what you intend to do than to past code that does not work. 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
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.