Save time and key code of each key stroke in a queue

I need keypress times for all presses while a visual and a audio task are running simultaneously. I do not have any point in my code to set a while loop for KbCheck and I need a passive queue to save each key stroke and its press time. KbEventGet is not useful because it returns GetSecs time of each press.

My code is like this:

while totalTime < 30 
    PsychPortAudio('AddToSchedule', pahandle, bufferhandle_beep, prtcpnt.beepTime);
    PsychPortAudio('AddToSchedule', pahandle, bufferhandle_attentive, prtcpnt.attentiveTime);
end
 PsychPortAudio('Start', pahandle);
    
my visual task 

I would appreciate it if anyone can show me the way!

I do not understand your statement " KbEventGet is not useful because it returns GetSecs time of each press". The time of KbEventGet is using the same clock as screen flips or audio timestamps.

I’m working on this code from github:

fprintf('5 of 6.  Testing KbQueueCheck asynchronously:\nPress one or more keys during the next 10 seconds.\n');
KbQueueCreate();
KbQueueStart();
WaitSecs(10);
fprintf('\n');
KbQueueStop();	% Stop delivering events to the queue
fprintf('Keypresses during the next 5 seconds will be ignored\n\n');
WaitSecs(5);
[ pressed, firstPress]=KbQueueCheck();
fprintf('You pressed the following keys during the interval when keypresses were being recorded:\n');
pressedKeys = KbName(firstPress)

fprintf('\n\nTesting KbEvent-Buffer. It should contain the same information.\n');
fprintf('During the test interval, %i events were recorded.\n', KbEventAvail());
fprintf('Will print all of them:\n\n');
while KbEventAvail()
    [evt, n] = KbEventGet();
    fprintf('Event is:\n'); disp(evt);
    fprintf('\nNow %i events remaining.\n', n);
end

fprintf('Done. Flushing the buffer for fun...\n');
n = KbEventFlush();
if n == 0
    fprintf('There were zero events remaining at flush time, as expected.\n');
else
    fprintf('Ohoh! There were %i events remaining, while i expected zero events?!?\n', n);
end
fprintf('KbEventBuffer test finished.\n\n');

KbQueueRelease();

and evt.time is the same for all keyboard events. Is that actually what the code should do or there is something wrong?

it works just fine for me. note that evt.Time is a huge number. compare the evt.Time from event 2-last with evt.Time from the 1st and you see that time is not the same.

Thanks for your quick answer. That was the point!