Not properly updating key presses

I have a piece of code that is supposed to collect key presses during a 2.5 second period. Every .5 seconds I am checking how many presses were made during that period. If the amount of presses exceeds the threshold, then I want to decrease the transparency of an image. Otherwise, it should stay the same. I am logging times of key presses as well as the final transparency level at the end of each trial. In my analysis however, I am noticing that the final transparency level sometimes is not what i am expecting based on times of presses. Sometimes it works, sometimes it does not. Any thoughts would be greatly appreciated. Thank you!

I see a potential issue. If you use KbCheck to count key presses, you will need to detect the key release whenever there is a press. Otherwise one press will be counted as multiple press.

Interesting. So for example, if someone made a press within a given .5 second window, and then made another press within that window, the second press may not be recorded by the program?

No, your loop checks the keypress many times in rapid succession. Each time that it sees the key is down, it adds one. So it doesn’t press individual key presses, but every time the check is executed and the key is down. You need to not count until the key has been released again.


Hi Dee, thanks for you response. What are the implications for actual logging of the key press times? Would this mean that we may be logging presses that are not actually occurring? Or not that we may not be logging all presses? Or both maybe?

What KbCheck reports is if a key is CURRENTLY DOWN, not the key press event. For you purpose, maybe it is better to use KbQueue or KbEvent series functions, although the syntax is a little complicated.

Sorry I am new to matlab and ptb. What do you mean by currently down?

A key is pressed down, and not yet released.

As in, you are currently holding it down, physically, on the keyboard. Your loop is checking that many times every second. So even a very short keypress will be found to be down for multiple iterations in your loop. Play with just that bit of code and see what it does with single presses, that may help you understand what is going on (e.g. print some text to the console every time a key is found to be depressed).