# GetMouse in a loop

**URL:** <https://psychtoolbox.discourse.group/t/getmouse-in-a-loop/604>\
**Category:** Uncategorized\
**Created:** [November 24, 2009, 7:17pm UTC](https://psychtoolbox.discourse.group/t/getmouse-in-a-loop/604 "2009-11-24T19:17:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [November 24, 2009, 7:17pm UTC](https://psychtoolbox.discourse.group/t/getmouse-in-a-loop/604/1 "2009-11-24T19:17:07Z")

</div>

&nbsp;

If I use GetMouse in a loop, as you can see below… the program is hanging up after one round…

&nbsp;

try

&nbsp;&nbsp;&nbsp; % Open up a window on the screen and clear it.

&nbsp;&nbsp;&nbsp; whichScreen = max(Screen('Screens'));

&nbsp;&nbsp;&nbsp; [theWindow,theRect] = Screen(whichScreen,'OpenWindow',0);

&nbsp;&nbsp;&nbsp; Priority(MaxPriority(0));

&nbsp;&nbsp;&nbsp; ListenChar(2);

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; n\_zaehler=3;

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; for zaehler=1:n\_zaehler;

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; Screen('TextFont',theWindow, 'Arial');

&nbsp;&nbsp;&nbsp; Screen('TextSize',theWindow, 24);

&nbsp;&nbsp;&nbsp; Screen('TextStyle', theWindow, 255);

&nbsp;&nbsp;&nbsp; DrawFormattedText(theWindow, 'Hit any key to go on', 'center', 708, 255);

&nbsp;&nbsp;&nbsp; Screen('Flip', theWindow);

&nbsp;&nbsp;&nbsp; KbWait;

&nbsp;&nbsp;&nbsp; WaitSecs(1.0);

&nbsp;&nbsp;&nbsp; Screen('Flip', theWindow);

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; % Move the cursor to the center of the screen

&nbsp;&nbsp;&nbsp; x = theRect(RectRight)/2;

&nbsp;&nbsp;&nbsp; y = theRect(RectBottom)/2;

&nbsp;&nbsp;&nbsp; SetMouse(x,y);

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; [x,y,buttons] = GetMouse(theWindow);

&nbsp;&nbsp;&nbsp; while any(buttons) % if already down, wait for release

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [x,y,buttons] = GetMouse(theWindow);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thePoints = [x, y];

&nbsp;&nbsp;&nbsp; end

&nbsp;&nbsp;&nbsp; while ~any(buttons) % wait for press

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [x,y,buttons] = GetMouse(theWindow);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thePoints = [x, y];

&nbsp;&nbsp;&nbsp; end

&nbsp;&nbsp;&nbsp; while any(buttons) % wait for release

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [x,y,buttons] = GetMouse(theWindow);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thePoints = [x, y];

&nbsp;&nbsp;&nbsp; end

&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; if x\<=theRect(RectRight)/2

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf ('the key is c');

&nbsp;&nbsp;&nbsp; else

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf ('the key is m');

&nbsp;&nbsp;&nbsp; end

&nbsp;

&nbsp;&nbsp;&nbsp; %Screen('Flip', theWindow);

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; ShowCursor;

&nbsp;&nbsp;&nbsp; WaitSecs(1.0);

&nbsp;&nbsp;&nbsp; end

&nbsp;

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; % Close up

&nbsp;&nbsp;&nbsp; ShowCursor;

&nbsp;&nbsp;&nbsp; ListenChar(0);

&nbsp;&nbsp;&nbsp; Screen(theWindow,'Close');

&nbsp;

catch

&nbsp;&nbsp;&nbsp; Screen('CloseAll')

&nbsp;&nbsp;&nbsp; ShowCursor;

&nbsp;&nbsp;&nbsp; psychrethrow(psychlasterror);

&nbsp;&nbsp;&nbsp; ListenChar(0);

end %try..catch..

&nbsp;

&nbsp;

I want to trace a movement with the mouse as a response in an experiment…

Got some idea?

Steffi
