Listening to keyboard (fmri scanner triggers) while playing videos

I’m using PTB with windows.
I’m presenting videos and fixation crosses and it works.
I also need to collect fmri triggers (aka keyboard presses) during the entire experiment.

I found a solution that suggests using a matlab figure to collect keystrokes:

h = figure; % create a figure to listen for key presses
set(h, 'KeyPressFcn', {@detectKeyPresses, tic}); % start listening for key presses

and then I’ve added this function to the video presentation loop. But it doesn’t allow to execute PTB video presentation if the figure is open.

Are there any solutions for this?

Normally for PTB you would use KbCheck / KbWait for common capture of key presses. This allows you to catch and check which keys are pressed and run code based on the keypress.

What is in the @detectKeyPresses anonymous fuinction?

Are you trying to passively capture keys in the background that are also being used by other software?

Here is the detectKeyPresses function:

function detectKeyPresses(KeyPressEvent, triggerKey, StartTime)
   global trigger_buffer % access the global keypress buffer
   if strcmp(KeyPressEvent.Key, triggerKey)
       % Store the key and its time in the buffer
       trigger_buffer(end+1).key = KeyPressEvent.Key;
       trigger_buffer(end).time = toc(StartTime);
       disp(['Key Pressed: ', KeyPressEvent.Key]); % print the key to the command window

I’ve looked at KbCheck / KbWait, but they don’t do what I need. There are triggers arriving from the MRI scanner during functional scan at every TR, so every 2 seconds in my case or so. And they are converted to button presses. I just need to record the exact time when this trigger arrives. My experiment doesn’t depend on it and shouldn’t be interrupted by it.

Well KbCheck checks if a key/button is pressed and explicitly returns the time this happened, so I don’t quite understand why it doesn’t do what you need. There are also the KbQueue... commands which record key presses in a queue to handle later for more precise timing.

The code in detectKeyPresses is plain MATLAB code, it isn’t PTB code; you suggest PTB is blocked from running with this figure open, and so this could be due to MATLAB’s lack of parallel execution (though I usually use MATLAB figures with callbacks alongside PTB code and haven’t observed this hanging, but I never tried a keypress callback). Can PTB run without that callback running?

If the problem is KbCheck not being able to “see” the keypresses coming from the scanner, then a paid request to Mario will be the fastest solution to your problem…

PTB code runs smooth without this request.
KbCheck can see the keypresses form the scanner, but it misses some of them. The solution would be to put KbCheck after every line of code of the experiment, but it doesn’t seem right and will potentially slw things down.

Will KbQueue family functions record all keypresses and timing? From examples, I understood that I still need to run KbQueueCheck to get actual presses and if there will be several presses of the same button, only the first and the last one will be saved, not all of them.

Use KbQueue function of families with KbEventGet. that can read all events, also same key multiple times, at a later time.