KbCheck in while loop - recording button presses several times

Good afternoon,

In my task (pseudo code below), I use a while loop (that runs for ~90sec until a block is done) in which the screen is continuously redrawn to show smooth motion.

I want to detect in that loop whether participants press buttons (and which ones) - they might be pressing a sequence of several buttons. I’m using kbcheck for this. This works. The problem is that sometimes button presses get detected more than once (e.g. I pressed ‘k’ once, but in my log file it’s listed 2x).

How can I solve this? Here are some options I’ve thought about, but I’m not sure which one would be best:

  • put in a forced pause (e.g. 20 msec) after a button press is detected. This works - Is there any that could go wrong with this?
  • store the time of the button press and record future presses only if it’s e.g. more than 20msec after the previous button press
  • should I use the “Flip” command with a specified time rather than run the while loop as quickly as possible?
  • Is there a better command than the KbCheck → if keycode(…) combination of commands that I can use? E.g. is there somehow a way to delete keyCode automatically after I’ve compared it? I guess I could myself set it to empty after a button press has been detected?

Pseudocode:

while GetSecs < finishTime
    % Draw screen to produce smooth motion
    draw_my_screen() % lots of commands to compute what should be on the screen at each time point t
    Flip('Screen',my_window)
    
    % Check for button press
    [keyIsDown,block_vars.currentTime ,keyCode] = KbCheck;  % check button presses
    if keyCode(my_key_of_interest)
        % Save the key press to my data matrix
        save_key(my_key_of_interest)
        
    end
end

Environment:
macOS Catalina (10.15.7)
Matlab 2018a
Pyschtoolbox version: 3.0.17

========================================================================================

H77ZC-NL-202192413322:cc231de4e3094ca12b931ef0ced579a366890ed8ba2031cf6f784136bb7fa954

========================================================================================

A good solution would be using KbQueue instead (there are demos starting with that in the name). The problem you have now is that KbCheck tells you if the key is currently down when you call it. Its likely down for multiple frames, thus its flagged multiple times for each press. You can write your own logic to not detect a new keypress until the key has been released again (pseudocode written on phone, you have to fix capitalization and perhaps other things):

IsDepressed = false;
% In loop:
If kbcheck && ~IsDepressed
% Flag key
IsDepressed = true;
Else
IsDepressed=false;
End

1 Like

Excellent, thanks so much!! KbQueue was just the set of commands I’ve been looking for. I’ll do some more extensive testing whether it all really works as it should, but for now it looks good.

Diedericks advice is th correct one.

Btw. your priority support is used up, as the question you asked a while ago already exceeded the 60 minutes of total time contingent, clocking in at 73 minutes. You’d have to buy a new license for further support by myself.

Also, relating to your old audio question: You could UpdatePsychtoolbox to PTB 3.0.18. It had some improvements to the PsychPortAudio sound driver, which may be able to work around macOS audio volume deficiencies. Seemed to work on my Catalina MBP 2017, maybe it also helps on yours.

For reference, this was the verdict under PTB 3.0.17 + macOS:

-mario

Thanks, sorry, yes I saw this after I had posted… Hopefully my experiment is now all good to go.