How to escape out of GetClicks

Hello,
I am trying to write code (using Matlab 2020, Windows 10, PTB 3) which captures the reaction time of a left or right mouseclick within a fixed duration of 3 seconds. It seems like the function ‘GetClicks’ would be good for this, however, it indefinitely waits for a click even when I put it within a while loop, so if the mouse is never clicked, the program will never proceed. Any suggestions/pointers on how to automatically proceed past the GetClicks line after 3 seconds regardless of whether there is a mouse click or not would be greatly appreciated.

trialTimeout=3;
startTime = Screen(‘Flip’, window1);
while GetSecs - startTime < trialTimeout
[clicks,x,y,whichButton] = GetClicks(window1);
respTime = GetSecs; %never gets to here if no mouse click
end

~Andy

Use a function like ´KbWait´, which has an optional timeout parameter.
While it is called Kb* suggesting its for keyboards, it also works for
mice. To know what device number to pass to have it listen for your
mouse, use ´GetMouseIndices´. See help KbWait for the info you may
need

Hi, thank you for your reply. I was able to use the GetMouse function for what I needed:

startTime = Screen(‘Flip’, window1);
trialTimeout = 3;
while GetSecs - startTime < trialTimeout
GetSecs;%%
[x,y,buttons,focus,valuators,valinfo] = GetMouse();
press_right = buttons(1);
press_left = buttons(3);
if press_right == 1
break
elseif press_left == 1
break
end