# Psychtoolbox version of waitforbuttonpress

**URL:** <https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525>\
**Category:** Uncategorized\
**Created:** [July 29, 2005, 7:41pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525 "2005-07-29T19:41:19Z")\
**Posts on this page:** 9\
**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:** [July 29, 2005, 7:41pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/1 "2005-07-29T19:41:19Z")

</div>

I was wondering if anyone knew of a psychtoolbox version of  
waitforbuttonpress. The function just pauses the program until a mouse  
button is clicked. So far, I have discovered ASK (which displays text  
in the top right corner, which I don't want) and getClicks (which  
pauses for a little while even after you click because its waiting to  
count additional clicks). I'm currently using getClicks, but I don't  
want to additional pause if there is a function that will do exactly  
what waitforbuttonpress does for figures. Thanks!  
  
Jessica   
  
Brandeis University

---

<div class="post-metadata">

**Author:** ![dfencsik](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dfencsik](https://psychtoolbox.discourse.group/u/dfencsik)\
**Post date:** [July 30, 2005, 1:45am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/2 "2005-07-30T01:45:21Z")

</div>

You can use GetMouse to check for a button press, but you have to  
check it repeatedly in a loop. I use the below function to wait for a  
mouse click in OS. It first waits until the mouse button is released  
(in case it's still down from a previous response), then waits for it  
to get pressed again. It's pretty minimalist, and doesn't return the  
mouse position, keys pressed, or any timing information.  
  
function MouseWait  
  
[x, y, button] = GetMouse;  
while any(button)  
 [x, y, button] = GetMouse;  
end  
while ~any(button)  
 [x, y, button] = GetMouse;  
end  
  
  
David  
  

> On 7/29/05, sabredanse \<[jmaryott@...](mailto:jmaryott@...)\> wrote:  
> \> I was wondering if anyone knew of a psychtoolbox version of  
> \> waitforbuttonpress. The function just pauses the program until a mouse  
> \> button is clicked. So far, I have discovered ASK (which displays text  
> \> in the top right corner, which I don't want) and getClicks (which  
> \> pauses for a little while even after you click because its waiting to  
> \> count additional clicks). I'm currently using getClicks, but I don't  
> \> want to additional pause if there is a function that will do exactly  
> \> what waitforbuttonpress does for figures. Thanks!  
> \>   
> \> Jessica  
> \>   
> \> Brandeis University  
>    
> --   
> David Fencsik, Ph.D.  
> Research Fellow  
>    
> Visual Attention Laboratory  
> Brigham and Women's Hospital  
> 64 Sidney St., Suite 170  
> Cambridge, MA 02139  
>    
> Phone: 617-768-8812  
> Email: [fencsik@...](mailto:fencsik@...)  
> Web: [http://search.bwh.harvard.edu/](http://search.bwh.harvard.edu/)

---

<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:** [July 31, 2005, 11:47pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/3 "2005-07-31T23:47:22Z")

</div>

Try waitforMouse, on my website.  
  
[http://csclab.ucsd.edu/~alan/vision/matlab\_code/](http://csclab.ucsd.edu/~alan/vision/matlab_code/)  
  
sabredanse wrote:  

> \> I was wondering if anyone knew of a psychtoolbox version of  
> \> waitforbuttonpress. The function just pauses the program until a mouse  
> \> button is clicked. So far, I have discovered ASK (which displays text  
> \> in the top right corner, which I don't want) and getClicks (which  
> \> pauses for a little while even after you click because its waiting to  
> \> count additional clicks). I'm currently using getClicks, but I don't  
> \> want to additional pause if there is a function that will do exactly  
> \> what waitforbuttonpress does for figures. Thanks!  
> \>   
> \> Jessica   
> \>   
> \> Brandeis University  
> \>   
> \>   
> \>   
> \>   
> \> Post your message to: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> \> Denis Pelli, David Brainard, and Allen Ingling.  
> \> [http://psychtoolbox.org](http://psychtoolbox.org)  
> \>   
> \> Yahoo! Groups Links  
> \>   
> \>   
> \>   
> \>   
> \>   
> \>   
> \>   
> \>

---

<div class="post-metadata">

**Author:** ![sabredanse](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sabredanse](https://psychtoolbox.discourse.group/u/sabredanse)\
**Post date:** [August 1, 2005, 2:17pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/4 "2005-08-01T14:17:09Z")

</div>

Thanks, this will help,  
  
Jessica  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), David Fencsik \<fencsik@g...\> wrote:  
> \> You can use GetMouse to check for a button press, but you have to  
> \> check it repeatedly in a loop. I use the below function to wait for a  
> \> mouse click in OS. It first waits until the mouse button is released  
> \> (in case it's still down from a previous response), then waits for it  
> \> to get pressed again. It's pretty minimalist, and doesn't return the  
> \> mouse position, keys pressed, or any timing information.  
> \>   
> \> function MouseWait  
> \>   
> \> [x, y, button] = GetMouse;  
> \> while any(button)  
> \> [x, y, button] = GetMouse;  
> \> end  
> \> while ~any(button)  
> \> [x, y, button] = GetMouse;  
> \> end  
> \>   
> \>   
> \> David  
> \>   
> \> On 7/29/05, sabredanse \<jmaryott@b...\> wrote:  
> \> \> I was wondering if anyone knew of a psychtoolbox version of  
> \> \> waitforbuttonpress. The function just pauses the program until a mouse  
> \> \> button is clicked. So far, I have discovered ASK (which displays text  
> \> \> in the top right corner, which I don't want) and getClicks (which  
> \> \> pauses for a little while even after you click because its waiting to  
> \> \> count additional clicks). I'm currently using getClicks, but I don't  
> \> \> want to additional pause if there is a function that will do exactly  
> \> \> what waitforbuttonpress does for figures. Thanks!  
> \> \>   
> \> \> Jessica  
> \> \>   
> \> \> Brandeis University  
> \>   
> \> --   
> \> David Fencsik, Ph.D.  
> \> Research Fellow  
> \>   
> \> Visual Attention Laboratory  
> \> Brigham and Women's Hospital  
> \> 64 Sidney St., Suite 170  
> \> Cambridge, MA 02139  
> \>   
> \> Phone: 617-768-8812  
> \> Email: fencsik@g...  
> \> Web: [http://search.bwh.harvard.edu/](http://search.bwh.harvard.edu/)

---

<div class="post-metadata">

**Author:** ![sabredanse](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sabredanse](https://psychtoolbox.discourse.group/u/sabredanse)\
**Post date:** [August 1, 2005, 2:18pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/5 "2005-08-01T14:18:48Z")

</div>

Thank you - I'll be able to use a slimmed down version of this to do  
exactly what I need.  
  
Jessica  
  
--- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), Alan Robinson \<alanAtHome@h...\>  
wrote:  

> \> Try waitforMouse, on my website.  
> \>   
> \> [http://csclab.ucsd.edu/~alan/vision/matlab\_code/](http://csclab.ucsd.edu/~alan/vision/matlab_code/)  
> \>   
> \> sabredanse wrote:  
> \> \> I was wondering if anyone knew of a psychtoolbox version of  
> \> \> waitforbuttonpress. The function just pauses the program until a mouse  
> \> \> button is clicked. So far, I have discovered ASK (which displays text  
> \> \> in the top right corner, which I don't want) and getClicks (which  
> \> \> pauses for a little while even after you click because its waiting to  
> \> \> count additional clicks). I'm currently using getClicks, but I don't  
> \> \> want to additional pause if there is a function that will do exactly  
> \> \> what waitforbuttonpress does for figures. Thanks!  
> \> \>   
> \> \> Jessica   
> \> \>   
> \> \> Brandeis University  
> \> \>   
> \> \>   
> \> \>   
> \> \>   
> \> \> Post your message to: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \> \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> \> \> Denis Pelli, David Brainard, and Allen Ingling.  
> \> \> [http://psychtoolbox.org](http://psychtoolbox.org)  
> \> \>   
> \> \> Yahoo! Groups Links  
> \> \>   
> \> \>   
> \> \>   
> \> \>   
> \> \>   
> \> \>   
> \> \>   
> \> \>

---

<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:** [August 4, 2005, 8:08am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/6 "2005-08-04T08:08:13Z")

</div>

Hi there,  
  
I have written code that simply flashes different ovals on the screen. If I do the ovals in color, frames are dropped (consistently 60 frames in the below demo), but if I do it in gray scale, it works fine (consistently drops 0 frames). I assume I am not using color optimally, but can't quite figure it out. My code for the colored circles is below. Does anyone have any advice?  
  
thanks a bunch,  
Maria Mckinley  
  
  
% MailListDemo  
% adapted from Psychtoolbox MovieDemo2  
% show on main screen  
whichScreen=0;  
pixelSizes=Screen(whichScreen,'PixelSizes');  
nFrames=round(.5\*FrameRate(whichScreen));  
priorityLevel=MaxPriority(whichScreen,'GetSecs','WaitBlanking');  
  
max(pixelSizes)  
% Open screen. Do this before opening the  
% offscreen windows so you can align offscreen  
[window,windowRect]=Screen(whichScreen,'OpenWindow',0,[],max(pixelSizes));  
% Setting clut seems to make no difference.  
onClut=(0:255)'\*[1 1 1];  
Screen(window,'SetClut',onClut);  
  
  
%%% THIS CODE IS JUST TO GET THE COORDINATES FOR THE RECTANGLE TO USE FOR THE OVAL  
% adapted from shadlendots  
% viewing width of monitor (cm)  
mon\_horizontal\_cm = 34;  
% distance from the center of the subject's eyes to the monitor (cm)  
view\_dist\_cm = 60;  
pixPerDeg = windowRect(3) \* ...  
 (1 ./ (2 \* atan2(mon\_horizontal\_cm / 2, view\_dist\_cm))) \* ...  
 pi/180;  
half\_width = windowRect(3)/2;  
half\_height = windowRect(4)/2;  
ppd = pixPerDeg/10;  
% the first circle will have diameter 30, centered  
xyd = [0 0 30];  
d\_ppd = xyd(:, 3) \* ppd;  
x0 = half\_width + xyd(:, 1) \* ppd - 0.5 \* d\_ppd;  
y0 = half\_height - xyd(:, 2) \* ppd - 0.5 \* d\_ppd;  
destRect = [x0 y0 (x0 + d\_ppd) (y0 + d\_ppd)];  
% the second circle will have diameter 30, to the left and up of the center  
xyd = [10 10 30];  
d\_ppd = xyd(:, 3) \* ppd;  
x0 = half\_width + xyd(:, 1) \* ppd - 0.5 \* d\_ppd;  
y0 = half\_height - xyd(:, 2) \* ppd - 0.5 \* d\_ppd;  
destRect2 = [x0 y0 (x0 + d\_ppd) (y0 + d\_ppd)];  
  
  
% Put images into offscreen memory. When we open an offscreen  
% window we specify which on-screen window it should be  
% aligned with, for fast copying.  
  
ow1=Screen(window,'OpenOffscreenWindow',0,windowRect);  
Screen(ow1,'FillOval', [255 0 0], destRect);  
  
ow2=Screen(window,'OpenOffscreenWindow',0,windowRect);  
Screen(ow2,'FillOval', [0 0 255], destRect2);  
  
% Set up string for Rush. You can make this as long as you like.  
% The initial call to WaitBlanking synchronizes the program with the display blanking.  
missed1=zeros(nFrames,1);  
missed2=zeros(nFrames,1);  
GetSecs; % Preload all functions.  
  
loop={  
 'Screen(window,''WaitBlanking'');'  
 'startTime=GetSecs;'  
 'for i=1:nFrames;'  
 'Screen(''CopyWindow'', ow1, window,windowRect,windowRect,''srcCopy'');'  
 'missed1(i)=Screen(window,''WaitBlanking'');'  
 'Screen(''CopyWindow'', ow2, window,windowRect,windowRect,''srcCopy'');'  
 'missed2(i)=Screen(window,''WaitBlanking'');'  
 'end;'  
 'finishTime=GetSecs;'  
};  
  
% Show the movie. The Rushed code does the timing.  
HideCursor;  
Screen('Screens');GetSecs; % Make sure all Rushed functions are in memory.  
Rush(loop,priorityLevel);  
ShowCursor;  
  
% Close up  
Screen('CloseAll');  
  
% Print results  
fprintf('MovieDemo4\n');  
s=sprintf('Movie is %.0f Hz.',FrameRate(whichScreen));  
s=sprintf('%s Copied %.0f images/sec.',s,2\*nFrames/(finishTime-startTime));  
s=sprintf('%s Missed %g frames.',s,sum(sum([missed1 missed2])));  
fprintf('%s\n',WrapString(s));  
  
RestoreScreen(whichScreen); % As of 7/31/02, FrameRate may leave CLUT in strange state.

---

<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:** [August 5, 2005, 6:43pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/7 "2005-08-05T18:43:40Z")

</div>

On Fri, 5 Aug 2005, David Fencsik wrote:  
  

> \> Maria,  
> \>  
> \> In your sample code, the ovals are drawn to off-screen windows that  
> \> are the same size as the on-screen window. This means that you are  
> \> copying a whole lot of unnecessary bytes every time a stimulus is  
> \> copied to the screen. Try making the off-screen stimulus windows as  
> \> small as possible (i.e., the max radius of the ovals).  
> \>  
> \> David  
> \>

Hi David,  
  
Thanks for the advice. The reason I did that is because I was having problems with the rectangles showing up when stimuli overlapped, if I didn't. Using the large off-screen window did not cause a timing problem in the black and white version, so I thought it should work with the color too. I really don't understand why it wouldn't, but if you have another idea of how to do this without the rectangles showing up, that would be great. I'm including some code that demonstrates what I mean about the rectangles.  
  
Tyi, this code will eventually be incorporated in a gui, in which the user is able to make the two circles any size, and put them anywhere on the screen (including one on top of the other, as in this demo). Sometimes they will both be shown at the same time, and sometimes just one at a time.  
  
thanks,  
Maria  
  
% MailListDemo2  
% adapted from Psychtoolbox MovieDemo2  
% show on main screen  
whichScreen=0;  
nFrames=round(0.5\*FrameRate(whichScreen));  
priorityLevel=MaxPriority(whichScreen,'GetSecs','WaitBlanking');  
  
% Open screen. Do this before opening the  
% offscreen windows so you can align offscreen  
% window memory to onscreen for faster copying.  
[window,windowRect]=Screen(whichScreen,'OpenWindow',0,[],8);  
  
onClut=(0:255)'\*[1 1 1];  
Screen(window,'SetClut',onClut);  
imageRect=SetRect(0,0,200,200);  
imageRect2=SetRect(0,0,300,300);  
destRect=CenterRect(imageRect,windowRect);  
destRect2=CenterRect(imageRect2,windowRect);  
% Put images into offscreen memory. When we open an offscreen  
% window we specify which on-screen window it should be  
% aligned with, for fast copying.  
  
ow1=Screen(window,'OpenOffscreenWindow',0,imageRect,8);  
Screen(ow1,'FillOval', 100, imageRect);  
  
ow2=Screen(window,'OpenOffscreenWindow',0,imageRect2,8);  
Screen(ow2,'FillOval', 255, imageRect2);  
  
% Set up string for Rush. You can make this as long as you like.  
% The initial call to WaitBlanking synchronizes the program with the display blanking.  
missed1=zeros(nFrames,1);  
missed2=zeros(nFrames,1);  
GetSecs;   
% Preload all functions.  
loop={  
 'Screen(window,''WaitBlanking'');'  
 'startTime=GetSecs;'  
 'for i=1:nFrames;'  
 'Screen(''CopyWindow'', ow1, window,imageRect,destRect,''srcCopy'');'  
 'missed1(i)=Screen(window,''WaitBlanking'');'  
 'Screen(''CopyWindow'', ow2, window,imageRect2,destRect2,''srcCopy'');'  
 'missed2(i)=Screen(window,''WaitBlanking'');'  
 'end;'  
 'finishTime=GetSecs;'  
};  
  
% Show the movie. The Rushed code does the timing.  
HideCursor;  
Screen('Screens');GetSecs; % Make sure all Rushed functions are in memory.  
Rush(loop,priorityLevel);  
ShowCursor;  
  
% Close up  
Screen('CloseAll');  
  
% Print results  
fprintf('MovieDemo3\n');  
s=sprintf('Movie is %.0f Hz.',FrameRate(whichScreen));  
s=sprintf('%s Copied %.0f images/sec.',s,2\*nFrames/(finishTime-startTime));  
s=sprintf('%s Missed %g frames.',s,sum(sum([missed1 missed2])));  
fprintf('%s\n',WrapString(s));  
  
RestoreScreen(whichScreen);   
% As of 7/31/02, FrameRate may leave CLUT in strange state.

---

<div class="post-metadata">

**Author:** ![dfencsik](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dfencsik](https://psychtoolbox.discourse.group/u/dfencsik)\
**Post date:** [August 7, 2005, 12:45am UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/8 "2005-08-07T00:45:08Z")

</div>

Hi Maria,  
  
I think the reason you only observe dropped frames in color and not in  
black and white is that the computer has to copy three times as much  
information in color relative to b/w. There is only one byte per  
pixel in b/w vs. three bytes per pixel for color (one byte each for  
red, green, and blue levels).  
  
As for the visible rectangles, try specifying 'transparent' instead of  
'srcCopy' as the last argument to Screen('CopyWindow'), and make sure  
your ovals are drawn on a white background. To make sure they have a  
white background, open your offsreen windows like this:  
  
ow1=Screen(window,'OpenOffscreenWindow',[],imageRect);  
  
The 'transparent' argument instructs 'CopyWindow' to skip any white  
pixels in the source window. Of course, it will also skip any  
white-colored pixels within the oval, so you need to draw them in  
off-white. I draw white stimuli using a color value of 254 instead of  
255.  
  
Incidentally, does anybody have a better solution to transparently  
drawing white stimuli? Is there a method for specifying a different  
color to use as the transparent one?  
  
Hope that helps.  
  
David  
  
  
--   
David Fencsik, Ph.D.  
Research Fellow  
  
Visual Attention Laboratory  
Brigham and Women's Hospital  
64 Sidney St., Suite 170  
Cambridge, MA 02139  
  
Phone: 617-768-8812  
Email: [fencsik@...](mailto:fencsik@...)  
Web: [http://search.bwh.harvard.edu/](http://search.bwh.harvard.edu/)  
  
  

> On 8/5/05, Maria McKinley \<[parody@...](mailto:parody@...)\> wrote:  
> \>   
> \> Hi David,  
> \>   
> \> Thanks for the advice. The reason I did that is because I was having problems with the rectangles showing up when stimuli overlapped, if I didn't. Using the large off-screen window did not cause a timing problem in the black and white version, so I thought it should work with the color too. I really don't understand why it wouldn't, but if you have another idea of how to do this without the rectangles showing up, that would be great. I'm including some code that demonstrates what I mean about the rectangles.  
> \>   
> \> Tyi, this code will eventually be incorporated in a gui, in which the user is able to make the two circles any size, and put them anywhere on the screen (including one on top of the other, as in this demo). Sometimes they will both be shown at the same time, and sometimes just one at a time.  
> \>   
> \> thanks,  
> \> Maria

---

<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:** [August 7, 2005, 9:19pm UTC](https://psychtoolbox.discourse.group/t/psychtoolbox-version-of-waitforbuttonpress/1525/9 "2005-08-07T21:19:17Z")

</div>

Thanks David,  
  
Silly me, I was thinking you still had to do 3 matrices, but of course   
they would be the same matrix, so would actually only be one. Should   
have thought that one through more before I asked. I guess I had   
assumed that it would handle these matrices more intelligently (ie, most   
of my screen is black, so  
  
Definitely making progress, but this introduces a new question. The way   
I was doing things previously, if I wanted to show a target that was up   
on the previous screen, I just made an offscreen window that has both   
targets. Now I don't know how to do this. Trying to make an image   
rectangle that is big enough for both targets seems silly (and if they   
are in opposite corners, is the same as copying the whole screen and   
will be just as slow), but trying to copy multiple, sometimes   
overlapping, windows to one screen seems to introduce weird visual   
glitches, and I can't make it do exactly what I want anyway.  
  
Maybe I shouldn't be trying to make a movie at all. I think I will test   
to see how fast filloval is when drawing directly to the screen.  
  
If anyone has any further ideas, I'm all ears...  
  
cheers,  
maria  
  
David Fencsik wrote:  

> \> Hi Maria,  
> \>   
> \> I think the reason you only observe dropped frames in color and not in  
> \> black and white is that the computer has to copy three times as much  
> \> information in color relative to b/w. There is only one byte per  
> \> pixel in b/w vs. three bytes per pixel for color (one byte each for  
> \> red, green, and blue levels).  
> \>   
> \> As for the visible rectangles, try specifying 'transparent' instead of  
> \> 'srcCopy' as the last argument to Screen('CopyWindow'), and make sure  
> \> your ovals are drawn on a white background. To make sure they have a  
> \> white background, open your offsreen windows like this:  
> \>   
> \> ow1=Screen(window,'OpenOffscreenWindow',[],imageRect);  
> \>   
> \> The 'transparent' argument instructs 'CopyWindow' to skip any white  
> \> pixels in the source window. Of course, it will also skip any  
> \> white-colored pixels within the oval, so you need to draw them in  
> \> off-white. I draw white stimuli using a color value of 254 instead of  
> \> 255.  
> \>   
> \> Incidentally, does anybody have a better solution to transparently  
> \> drawing white stimuli? Is there a method for specifying a different  
> \> color to use as the transparent one?  
> \>   
> \> Hope that helps.  
> \>   
> \> David  
> \>   
> \>
