| Reference | Downloads | Github

Outputting number of button presses after a trial

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

OS : Win7
PsychoPy version : 2

**What are you trying to achieve?: We have a task where we control attention by occasionally flashing a red colored band of #### marks and ask the subject to respond with a button press (stored as Keypress: r). I would like to be able to store the number of times the subject presses the red button within 600 ms of the red hash marks. I would like to output at the end of the program the number of correct times “r” was pressed after the red hash marks. The hash marks are displayed in a loop where an .xls file is read and each line is a different set of symbols or words and corresponding color (most are black, with occasional red). Please let me know if I can provide more information to clarify. Thank you for your help.

Hi @tradman, are you using Builder or Coder to make your experiment? If you are using Builder, this is easily done using the keyboard component.

  • You want to know how many times the user pressed the button in a time window, so you would set the start and end duration of the keyboard to fill that time window. E.g., if you stim starts at 0ms and ends at 600ms of each trial, then 0 is your start time, and .6 is the duration of your keyboard, then it will only collect responses within that time.
  • You want to know whether the response was correct i.e., pressed on red not black. Add a column to your conditions file called “corrAns” and add the correct response from the keyboard, so r for red and b for black (if that is your key scheme). Then, let the keyboard component know what the correct answer is by feeding it the variable corrAns. Do this by selecting store correct and adding the corrAns variable to the Correct answer parameter. This will automatically add a column to your conditions file (e.g., keyboard.corr) that tells you whether or not the response was correct - 1 for correct, 0 for incorrect.

Thanks for your response. I have been using builder to make my experiment. The first challenge I foresee is the trials are of a shorter duration (155 ms) than the respond window (600 ms).

Also, is there a way to have psychopy count up the number of red button presses and display this at the completion of the programs run? It is still not clear to me how to do this.

Thank you.

Sorry, not sure what you mean. If your trial is 155ms, then your response window ends along with your trial, because the trial is over.

Sure, at the end of your experiment, you can access the data from the trialhandler. I take it you want to present some performance data at the end of the experiment? Here is an example, something to add to a code component:

# End routine tab
thisExp.addData("test", 1)  # I saved my data in a column called "test"

# Get all data
yourData = thisExp.getAllEntries()

# I want the data from the column "test"
allColData = []  # So I create a container for my data
[allColData.append(entry['test']) for entry in yourData]  # loop through all data, and append only the relevant data to my list/container.

print(allColData)  # Just so you can see

Sorry if I wasn’t clear. How the builder is constructed, the red button presses occur a number of trials after the red hash marks are displayed.

So how do I construct an if statement that increments a counter each time the red button is pressed?

I was hoping putting this in a code block would work:
(begin experiment)

(each frame)

theseKeys = event.getKeys(keyList=['r', '4', 'd'])
if len(theseKeys)>0:

But I think the key buffer is flushed by the time I am making this check. Can you let me know what the correct way to make this check of the red button press would be?

Sorry, just to be clear. Your participant sees some red hash marks, and when they do, they push a button multiple times in the same trial?

Each trial is either a red hash marks or black word. But a trial only lasts 155 ms. So when they some red hash marks they should press the red button, but usually the press occurs a few trials after the trial with red hash marks. The red hashes and button presses occur multiple times in an experiment, but once per trial of the loop of this builder routine.


I think I figured out the first step of counting the number of red button presses. It took a code block inside the looped routine:

(End Routine)

if ResponseForRed.keys != None:

Thanks for your help. Now I need a way of timestamping when the red text is displayed, and if the button press occurred within 600 ms of the red text. Is this feasible in psychopy?