# How to escape out of GetClicks

**URL:** <https://psychtoolbox.discourse.group/t/how-to-escape-out-of-getclicks/3470>\
**Category:** Programming Help\
**Created:** [October 27, 2020, 4:28pm UTC](https://psychtoolbox.discourse.group/t/how-to-escape-out-of-getclicks/3470 "2020-10-27T16:28:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![acoia](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@acoia](https://psychtoolbox.discourse.group/u/acoia)\
**Post date:** [October 27, 2020, 4:28pm UTC](https://psychtoolbox.discourse.group/t/how-to-escape-out-of-getclicks/3470/1 "2020-10-27T16:28:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [October 27, 2020, 9:55pm UTC](https://psychtoolbox.discourse.group/t/how-to-escape-out-of-getclicks/3470/2 "2020-10-27T21:55:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![acoia](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@acoia](https://psychtoolbox.discourse.group/u/acoia)\
**Post date:** [October 28, 2020, 4:24pm UTC](https://psychtoolbox.discourse.group/t/how-to-escape-out-of-getclicks/3470/3 "2020-10-28T16:24:14Z")

</div>

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
