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.