# continuous registering responses with KbCheck

**URL:** <https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771>\
**Category:** Uncategorized\
**Created:** [August 29, 2006, 5:01pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771 "2006-08-29T17:01:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vvanwass](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@vvanwass](https://psychtoolbox.discourse.group/u/vvanwass)\
**Post date:** [August 29, 2006, 5:01pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/1 "2006-08-29T17:01:50Z")

</div>

Hello List,   
  
I'd like to have responses recorded continuously while various stimuli  
are being presented (by recorded I mean (1) the key that was pressed  
and (2) the 'reaction time' from trial onset). There is no constraint  
as to when the participant is to respond (hence before, during or  
after each stimulus is valid). Each trial can be as long as 5 minutes.  
  
I am putting an excerpt of the script below. This is not currently  
doing the job. I have tried a couple different ways but somehow I  
never get the full set of responses. (i.e. if I test myself and press  
10 times a key during the period, only 2 of them might be recorded).  
  
I have tried to change the position of the KbCheck in and out of the  
loop, create a timing loop before and including the trial  
presentation...I am sure there is a very simple way to do this, but I  
can't seem to find it....  
  
Any help would be most appreciated!! Thanks in advance!  
  
(System: G5, Matlab 7.0.4 and either PsychToolboz v1.0.5 or v1.0.6)  
  
  
Actual Trial loop:  
  
%%%%%%%%%%%  
for i = 1:length(condOrder)  
  
 Screen('FillRect',w, black);  
 drawFixation(FIX\_COLOR);  
 Screen('Flip', w);  
  
 if condOrder(i) == 1  
 deviant = deviantC1;  
 devname = 'deviantC1';  
 [...]  
 else   
 deviant = deviantD3;  
 devname = 'deviantD3';  
 end  
  
 % ----- initialization variables ------  
 idx = 1;  
 j = 0;  
  
 rtime = [];   
 keyPresses=[];   
 keyTimes =[];   
 devTimes = [];  
  
 tic;  
 iterations = 20;  
 numdeviants = 3;  
  
 % randomly determine timing of deviants  
 tmp = Shuffle(1:5:iterations);   
 j\_rnd = sort(tmp(1:numdeviants));   
 % ----- end initialization variables ------  
  
  
 k = j\_rnd(idx);   
  
 TrialTime = GetSecs;  
  
  
 while j \<= iterations   
 k = j\_rnd(idx);   
  
 [keyIsDown,secs,keyCode]= KbCheck();   
 keyNumber = getKeyNumber(keyCode); % get numerical value  
  
 if ~ isempty(keyNumber)  
 keyPresses = [keyPresses keyNumber];  
 keyTimes= [keyTimes secs];  
 end  
  
 if j == k  
 devTime = GetSecs;  
 devTimes = [devTimes devTime];  
  
 sound (deviant,fs);  
 idx = idx + 1;  
  
 if idx \> length(j\_rnd)   
 idx = 1;   
 end  
 else  
 sound(triplet,fs);   
 end %if  
  
 j=j+1;  
  
 end %while j  
 endtime = clock;  
  
  
%%% sub-function, end of script   
%--------------------------------------------------------------------------  
function keyNumber = getKeyNumber( keyCode )  
 global ESC\_KEY  
 keyNumber=[];  
 for i=1:length(keyCode)  
 if keyCode(i)  
 keyName=upper(KbName(i)); %get the key name from the  
keyboard key index  
 if strcmp(keyName, ESC\_KEY)  
 error('ESCAPE key detected');  
 end  
 keyNumber=str2num(keyName); %turn the key name into a number  
 return; %return once the first key pressed is found  
 end   
 end  
%--------------------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![mvaziri\_p](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@mvaziri\_p](https://psychtoolbox.discourse.group/u/mvaziri_p)\
**Post date:** [August 29, 2006, 5:17pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/2 "2006-08-29T17:17:24Z")

</div>

Hi,  
  
 My experience with getting multiple keys all at once  
was that there is a limit in keyboard input. This  
depends on the keys that you choose. if the keys that  
you choose are in two different rows(2 in one and 2 in  
the other row like: a,z,k,m) never the four and usualy  
just two of the keypresses are recorded. But when you  
choose the keys so that all of them are in one row  
like: z,x,m,n (I have tested up to 4 keys I do not  
know about more) all of them will be recorded. I do  
not know exactly what is the technical problem with  
the keyboard but this is what I found by experiment.  
  
Hope it is helpull,  
Bests,  
Maryam.  
  
--- vvanwass \<[vvanwass@...](mailto:vvanwass@...)\> wrote:  
  

> \> Hello List,   
> \>   
> \> I'd like to have responses recorded continuously  
> \> while various stimuli  
> \> are being presented (by recorded I mean (1) the key  
> \> that was pressed  
> \> and (2) the 'reaction time' from trial onset). There  
> \> is no constraint  
> \> as to when the participant is to respond (hence  
> \> before, during or  
> \> after each stimulus is valid). Each trial can be as  
> \> long as 5 minutes.  
> \>   
> \> I am putting an excerpt of the script below. This is  
> \> not currently  
> \> doing the job. I have tried a couple different ways  
> \> but somehow I  
> \> never get the full set of responses. (i.e. if I test  
> \> myself and press  
> \> 10 times a key during the period, only 2 of them  
> \> might be recorded).  
> \>   
> \> I have tried to change the position of the KbCheck  
> \> in and out of the  
> \> loop, create a timing loop before and including the  
> \> trial  
> \> presentation...I am sure there is a very simple way  
> \> to do this, but I  
> \> can't seem to find it....  
> \>   
> \> Any help would be most appreciated!! Thanks in  
> \> advance!  
> \>   
> \> (System: G5, Matlab 7.0.4 and either PsychToolboz  
> \> v1.0.5 or v1.0.6)  
> \>   
> \>   
> \> Actual Trial loop:  
> \>   
> \> %%%%%%%%%%%  
> \> for i = 1:length(condOrder)  
> \>   
> \> Screen('FillRect',w, black);  
> \> drawFixation(FIX\_COLOR);  
> \> Screen('Flip', w);  
> \>   
> \> if condOrder(i) == 1  
> \> deviant = deviantC1;  
> \> devname = 'deviantC1';  
> \> [...]  
> \> else   
> \> deviant = deviantD3;  
> \> devname = 'deviantD3';  
> \> end  
> \>   
> \> % ----- initialization variables ------  
> \> idx = 1;  
> \> j = 0;  
> \>   
> \> rtime = [];   
> \> keyPresses=[];   
> \> keyTimes =[];   
> \> devTimes = [];  
> \>   
> \> tic;  
> \> iterations = 20;  
> \> numdeviants = 3;  
> \>   
> \> % randomly determine timing of deviants  
> \> tmp = Shuffle(1:5:iterations);   
> \> j\_rnd = sort(tmp(1:numdeviants));   
> \> % ----- end initialization variables ------  
> \>   
> \>   
> \> k = j\_rnd(idx);   
> \>   
> \> TrialTime = GetSecs;  
> \>   
> \>   
> \> while j \<= iterations   
> \> k = j\_rnd(idx);   
> \>   
> \> [keyIsDown,secs,keyCode]= KbCheck();   
> \> keyNumber = getKeyNumber(keyCode); %  
> \> get numerical value  
> \>   
> \> if ~ isempty(keyNumber)  
> \> keyPresses = [keyPresses keyNumber];  
> \> keyTimes= [keyTimes secs];  
> \> end  
> \>   
> \> if j == k  
> \> devTime = GetSecs;  
> \> devTimes = [devTimes devTime];  
> \>   
> \> sound (deviant,fs);  
> \> idx = idx + 1;  
> \>   
> \> if idx \> length(j\_rnd)   
> \> idx = 1;   
> \> end  
> \> else  
> \> sound(triplet,fs);   
> \> end %if  
> \>   
> \> j=j+1;  
> \>   
> \> end %while j  
> \> endtime = clock;  
> \>   
> \>   
> \> %%% sub-function, end of script   
> \>

%--------------------------------------------------------------------------  

> \> function keyNumber = getKeyNumber( keyCode )  
> \> global ESC\_KEY  
> \> keyNumber=[];  
> \> for i=1:length(keyCode)  
> \> if keyCode(i)  
> \> keyName=upper(KbName(i)); %get the key  
> \> name from the  
> \> keyboard key index  
> \> if strcmp(keyName, ESC\_KEY)  
> \> error('ESCAPE key detected');  
> \> end  
> \> keyNumber=str2num(keyName); %turn the  
> \> key name into a number  
> \> return; %return once the first key  
> \> pressed is found  
> \> end   
> \> end  
> \>

%--------------------------------------------------------------------------  

> \>   
> \>   
> \>   
> \>   
> \>

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around   
[http://mail.yahoo.com](http://mail.yahoo.com)

---

<div class="post-metadata">

**Author:** ![vvanwass](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@vvanwass](https://psychtoolbox.discourse.group/u/vvanwass)\
**Post date:** [August 29, 2006, 5:32pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/3 "2006-08-29T17:32:04Z")

</div>

Hi Maryam,   
  
 thanks for your input. The keys are either '1' or '2' on the num keypad. They are thus 'aligned'.  
  
 Thanks,   
 Virginie  
  
**_maryam vaziri-pashkam \<mvaziri\_p@...\>_** wrote:

> > Hi,  
> >   
> > My experience with getting multiple keys all at once  
> > was that there is a limit in keyboard input. This  
> > depends on the keys that you choose. if the keys that  
> > you choose are in two different rows(2 in one and 2 in  
> > the other row like: a,z,k,m) never the four and usualy  
> > just two of the keypresses are recorded. But when you  
> > choose the keys so that all of them are in one row  
> > like: z,x,m,n (I have tested up to 4 keys I do not  
> > know about more) all of them will be recorded. I do  
> > not know exactly what is the technical problem with  
> > the keyboard but this is what I found by experiment.  
> >   
> > Hope it is helpull,  
> > Bests,  
> > Maryam.  
> >   
> > --- vvanwass \<[vvanwass@yahoo. com](mailto:vvanwass%40yahoo.com)\> wrote:  
> >   
> > \> Hello List,   
> > \>   
> > \> I'd like to have responses recorded continuously  
> > \> while various stimuli  
> > \> are being presented (by recorded I mean (1) the key  
> > \> that was pressed  
> > \> and (2) the 'reaction time' from trial onset). There  
> > \> is no constraint  
> > \> as to when the participant is to respond (hence  
> > \> before, during or  
> > \> after each stimulus is valid). Each trial can be as  
> > \> long as 5 minutes.  
> > \>   
> > \> I am putting an excerpt of the script below. This is  
> > \> not currently  
> > \> doing the job. I have tried a couple different ways  
> > \> but somehow I  
> > \> never get the full set of responses. (i.e. if I test  
> > \> myself and press  
> > \> 10 times a key during the period, only 2 of them  
> > \> might be recorded).  
> > \>   
> > \> I have tried to change the position of the KbCheck  
> > \> in and out of the  
> > \> loop, create a timing loop before and including the  
> > \> trial  
> > \> presentation. ..I am sure there is a very simple way  
> > \> to do this, but I  
> > \> can't seem to find it....  
> > \>   
> > \> Any help would be most appreciated! ! Thanks in  
> > \> advance!  
> > \>   
> > \> (System: G5, Matlab 7.0.4 and either PsychToolboz  
> > \> v1.0.5 or v1.0.6)  
> > \>   
> > \>   
> > \> Actual Trial loop:  
> > \>   
> > \> %%%%%%%%%%%  
> > \> for i = 1:length(condOrder)  
> > \>   
> > \> Screen('FillRect' ,w, black);  
> > \> drawFixation( FIX\_COLOR) ;  
> > \> Screen('Flip' , w);  
> > \>   
> > \> if condOrder(i) == 1  
> > \> deviant = deviantC1;  
> > \> devname = 'deviantC1';  
> > \> [...]  
> > \> else   
> > \> deviant = deviantD3;  
> > \> devname = 'deviantD3';  
> > \> end  
> > \>   
> > \> % ----- initialization variables ------  
> > \> idx = 1;  
> > \> j = 0;  
> > \>   
> > \> rtime = [];   
> > \> keyPresses=[];   
> > \> keyTimes =[];   
> > \> devTimes = [];  
> > \>   
> > \> tic;  
> > \> iterations = 20;  
> > \> numdeviants = 3;  
> > \>   
> > \> % randomly determine timing of deviants  
> > \> tmp = Shuffle(1:5: iterations) ;   
> > \> j\_rnd = sort(tmp(1:numdevia nts));   
> > \> % ----- end initialization variables ------  
> > \>   
> > \>   
> > \> k = j\_rnd(idx);   
> > \>   
> > \> TrialTime = GetSecs;  
> > \>   
> > \>   
> > \> while j \<= iterations   
> > \> k = j\_rnd(idx);   
> > \>   
> > \> [keyIsDown,secs, keyCode]= KbCheck();   
> > \> keyNumber = getKeyNumber( keyCode); %  
> > \> get numerical value  
> > \>   
> > \> if ~ isempty(keyNumber)  
> > \> keyPresses = [keyPresses keyNumber];  
> > \> keyTimes= [keyTimes secs];  
> > \> end  
> > \>   
> > \> if j == k  
> > \> devTime = GetSecs;  
> > \> devTimes = [devTimes devTime];  
> > \>   
> > \> sound (deviant,fs) ;  
> > \> idx = idx + 1;  
> > \>   
> > \> if idx \> length(j\_rnd)   
> > \> idx = 1;   
> > \> end  
> > \> else  
> > \> sound(triplet, fs);   
> > \> end %if  
> > \>   
> > \> j=j+1;  
> > \>   
> > \> end %while j  
> > \> endtime = clock;  
> > \>   
> > \>   
> > \> %%% sub-function, end of script   
> > \>  
> > %----------- --------- --------- --------- --------- --------- --  
> > \> function keyNumber = getKeyNumber( keyCode )  
> > \> global ESC\_KEY  
> > \> keyNumber=[] ;  
> > \> for i=1:length(keyCode)  
> > \> if keyCode(i)  
> > \> keyName=upper( KbName(i) ); %get the key  
> > \> name from the  
> > \> keyboard key index  
> > \> if strcmp(keyName, ESC\_KEY)  
> > \> error('ESCAPE key detected');  
> > \> end  
> > \> keyNumber=str2num( keyName); %turn the  
> > \> key name into a number  
> > \> return; %return once the first key  
> > \> pressed is found  
> > \> end   
> > \> end  
> > \>  
> > %----------- --------- --------- --------- --------- --------- --  
> > \>   
> > \>   
> > \>   
> > \>   
> > \>   
> >   
> > \_\_\_\_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_ \_\_  
> > Do You Yahoo!?  
> > Tired of spam? Yahoo! Mail has the best spam protection around   
> > [http://mail. yahoo.com](http://mail.yahoo.com)
> 
>   
> 
> * * *
> Get your email and more, right on the [new Yahoo.com](http://us.rd.yahoo.com/evt=42973/*http://www.yahoo.com/preview)

---

<div class="post-metadata">

**Author:** ![anblick](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anblick](https://psychtoolbox.discourse.group/u/anblick)\
**Post date:** [August 29, 2006, 5:38pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/4 "2006-08-29T17:38:19Z")

</div>

Dear Virginie,
  

This seems like the problem then. You will need to have another loop within your iteration that continually calls KbCheck, with a waitsecs(0.001) also to ensure that the CPU isn't overloaded.

  

I'd suggest something like this:

  

exit\_time = GetSecs + iteration\_duration;

while(GetSecs \< exit\_time)

 [keyisdown, secs, keycode] = KbCheck;

 if keyisdown

 break

 end

 waitsecs(0.001)

end

  

This will continually poll your keyboard, breaking out of the loop once a key is pressed, or exiting when the specified duration has elapsed. You can do other things if you need to within the loop as well.

  

keith

  

On Aug 29, 2006, at 1:30 PM, Virginie van Wassenhove wrote:
  

> > Dear Keith,  
> >   
> > yes this is in fact an issue. I have tried to use the KbCheck within the 'if' statement (in addition to within the 'while') or create another 'while' statement for the KbCheck before the while pertaining to the iteration. None of these two worked.   
> >   
> > Each iteration is ~550ms but can vary with stimulus length, no longer than 1s -I don't expect to have multiple key presses during that time.  
> >   
> > Thanks,   
> > Virginie  
> > **_Keith Schneider \<[ks@...](mailto:ks@...)\>_** wrote:
> > 
> > > Dear Vvanwass,  
> > >   
> > > Looking at your script briefly, it looks like you are polling the   
> > > keyboard only once per "iteration". How long is each iteration? You   
> > > should be polling the keyboard every few milliseconds at least.   
> > > Remember, the KbCheck function only tells you whether a key is   
> > > depressed at the instant that the function is called.  
> > >   
> > > keith  
> > >   
> > > On Aug 29, 2006, at 1:01 PM, vvanwass wrote:  
> > >   
> > > \> Hello List,  
> > > \>  
> > > \> I'd like to have responses recorded continuously while various stimuli  
> > > \> are being presented (by recorded I mean (1) the key that was pressed  
> > > \> and (2) the 'reaction time' from trial onset). There is no constraint  
> > > \> as to when the participant is to respond (hence before, during or  
> > > \> after each stimulus is valid). Each trial can be as long as 5 minutes.  
> > > \>  
> > > \> I am putting an excerpt of the script below. This is not currently  
> > > \> doing the job. I have tried a couple different ways but somehow I  
> > > \> never get the full set of responses. (i.e. if I test myself and press  
> > > \> 10 times a key during the period, only 2 of them might be recorded).  
> > > \>  
> > > \> I have tried to change the position of the KbCheck in and out of the  
> > > \> loop, create a timing loop before and including the trial  
> > > \> presentation. ..I am sure there is a very simple way to do this, but I  
> > > \> can't seem to find it....  
> > > \>  
> > > \> Any help would be most appreciated! ! Thanks in advance!  
> > > \>  
> > > \> (System: G5, Matlab 7.0.4 and either PsychToolboz v1.0.5 or v1.0.6)  
> > > \>  
> > > \>  
> > > \> Actual Trial loop:  
> > > \>  
> > > \> %%%%%%%%%%%  
> > > \> for i = 1:length(condOrder)  
> > > \>  
> > > \> Screen('FillRect' ,w, black);  
> > > \> drawFixation( FIX\_COLOR) ;  
> > > \> Screen('Flip' , w);  
> > > \>  
> > > \> if condOrder(i) == 1  
> > > \> deviant = deviantC1;  
> > > \> devname = 'deviantC1';  
> > > \> [...]  
> > > \> else  
> > > \> deviant = deviantD3;  
> > > \> devname = 'deviantD3';  
> > > \> end  
> > > \>  
> > > \> % ----- initialization variables ------  
> > > \> idx = 1;  
> > > \> j = 0;  
> > > \>  
> > > \> rtime = [];  
> > > \> keyPresses=[];  
> > > \> keyTimes =[];  
> > > \> devTimes = [];  
> > > \>  
> > > \> tic;  
> > > \> iterations = 20;  
> > > \> numdeviants = 3;  
> > > \>  
> > > \> % randomly determine timing of deviants  
> > > \> tmp = Shuffle(1:5: iterations) ;  
> > > \> j\_rnd = sort(tmp(1:numdevia nts));  
> > > \> % ----- end initialization variables ------  
> > > \>  
> > > \>  
> > > \> k = j\_rnd(idx);  
> > > \>  
> > > \> TrialTime = GetSecs;  
> > > \>  
> > > \>  
> > > \> while j \<= iterations  
> > > \> k = j\_rnd(idx);  
> > > \>  
> > > \> [keyIsDown,secs, keyCode]= KbCheck();  
> > > \> keyNumber = getKeyNumber( keyCode); % get numerical value  
> > > \>  
> > > \> if ~ isempty(keyNumber)  
> > > \> keyPresses = [keyPresses keyNumber];  
> > > \> keyTimes= [keyTimes secs];  
> > > \> end  
> > > \>  
> > > \> if j == k  
> > > \> devTime = GetSecs;  
> > > \> devTimes = [devTimes devTime];  
> > > \>  
> > > \> sound (deviant,fs) ;  
> > > \> idx = idx + 1;  
> > > \>  
> > > \> if idx \> length(j\_rnd)  
> > > \> idx = 1;  
> > > \> end  
> > > \> else  
> > > \> sound(triplet, fs);  
> > > \> end %if  
> > > \>  
> > > \> j=j+1;  
> > > \>  
> > > \> end %while j  
> > > \> endtime = clock;  
> > > \>  
> > > \>  
> > > \> %%% sub-function, end of script  
> > > \> %----------- --------- --------- --------- --------- --------- --   
> > > \> ----  
> > > \> function keyNumber = getKeyNumber( keyCode )  
> > > \> global ESC\_KEY  
> > > \> keyNumber=[] ;  
> > > \> for i=1:length(keyCode)  
> > > \> if keyCode(i)  
> > > \> keyName=upper( KbName(i) ); %get the key name from the  
> > > \> keyboard key index  
> > > \> if strcmp(keyName, ESC\_KEY)  
> > > \> error('ESCAPE key detected');  
> > > \> end  
> > > \> keyNumber=str2num( keyName); %turn the key name into a   
> > > \> number  
> > > \> return; %return once the first key pressed is found  
> > > \> end  
> > > \> end  
> > > \> %----------- --------- --------- --------- --------- --------- --   
> > > \> ----  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \> Post your message to: [psychtoolbox@](mailto:psychtoolbox%40yahoogroups.com) yahoogroups. com  
> > > \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> > > \> Denis Pelli, David Brainard, and Allen Ingling.  
> > > \> [http://psychtoolbox](http://psychtoolbox.org) .org  
> > > \>  
> > > \> Yahoo! Groups Links  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \>  
> > > \>
> > 
> >   
> > 
> >   
> > 
> > * * *
> > Do you Yahoo!?  
> > Everyone is raving about the [all-new Yahoo! Mail.](http://us.rd.yahoo.com/evt=42297/*http://advision.webevents.yahoo.com/mailbeta)

---

<div class="post-metadata">

**Author:** ![vvanwass](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@vvanwass](https://psychtoolbox.discourse.group/u/vvanwass)\
**Post date:** [August 29, 2006, 8:04pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/5 "2006-08-29T20:04:08Z")

</div>

Hi Keith,   
  
 thanks for the code snippet. I have tried to implement it (within the iteration loop) but it is still not stable. It seems that a WaitSecs of 1ms is too short -i.e. it does not seem to flush out the previous keypresses - and 10 to 100ms either partially reports the key presses, not at all, or too many....  
 I'll try and work around these things some more  
  
 Thanks again!  
 Virginie  
  
**_Keith Schneider \<ks@...\>_** wrote:

> > Dear Virginie,
> > 
> >   
> > 
> > This seems like the problem then.&nbsp; You will need to have another loop within your iteration that continually calls KbCheck, with a waitsecs(0.001) also to ensure that the CPU isn't overloaded.
> > 
> >   
> > 
> > I'd suggest something like this:
> > 
> >   
> > 
> > exit\_time = GetSecs + iteration\_duration;
> > 
> > while(GetSecs \< exit\_time)
> > 
> > &nbsp; &nbsp;[keyisdown, secs, keycode] = KbCheck;
> > 
> > &nbsp; &nbsp;if keyisdown
> > 
> > &nbsp;&nbsp; &nbsp;break
> > 
> > &nbsp; end
> > 
> > &nbsp; waitsecs(0.001)
> > 
> > end
> > 
> >   
> > 
> > This will continually poll your keyboard, breaking out of the loop once a key is pressed, or exiting when the specified duration has elapsed.&nbsp; You can do other things if you need to within the loop as well.
> > 
> >   
> > 
> > keith
> > 
> >   
> > 
> > On Aug 29, 2006, at 1:30 PM, Virginie van Wassenhove wrote:
> >   
> > 
> > > Dear Keith,  
> > >   
> > > yes this is in fact an issue. I have tried to use the KbCheck within the 'if' statement (in addition to within the 'while') or create another 'while' statement for the KbCheck before the while pertaining to the iteration. None of these two worked.   
> > >   
> > > Each iteration is ~550ms but can vary with stimulus length, no longer than 1s -I don't expect to have multiple key presses during that time.  
> > >   
> > > Thanks,   
> > > Virginie  
> > > **_Keith Schneider \<[ks@.... edu](mailto:ks@...)\>_** wrote:
> > > 
> > > > Dear Vvanwass,  
> > > >   
> > > > Looking at your script briefly, it looks like you are polling the   
> > > > keyboard only once per "iteration". How long is each iteration? You   
> > > > should be polling the keyboard every few milliseconds at least.   
> > > > Remember, the KbCheck function only tells you whether a key is   
> > > > depressed at the instant that the function is called.  
> > > >   
> > > > keith  
> > > >   
> > > > On Aug 29, 2006, at 1:01 PM, vvanwass wrote:  
> > > >   
> > > > \> Hello List,  
> > > > \>  
> > > > \> I'd like to have responses recorded continuously while various stimuli  
> > > > \> are being presented (by recorded I mean (1) the key that was pressed  
> > > > \> and (2) the 'reaction time' from trial onset). There is no constraint  
> > > > \> as to when the participant is to respond (hence before, during or  
> > > > \> after each stimulus is valid). Each trial can be as long as 5 minutes.  
> > > > \>  
> > > > \> I am putting an excerpt of the script below. This is not currently  
> > > > \> doing the job. I have tried a couple different ways but somehow I  
> > > > \> never get the full set of responses. (i.e. if I test myself and press  
> > > > \> 10 times a key during the period, only 2 of them might be recorded).  
> > > > \>  
> > > > \> I have tried to change the position of the KbCheck in and out of the  
> > > > \> loop, create a timing loop before and including the trial  
> > > > \> presentation. ..I am sure there is a very simple way to do this, but I  
> > > > \> can't seem to find it....  
> > > > \>  
> > > > \> Any help would be most appreciated! ! Thanks in advance!  
> > > > \>  
> > > > \> (System: G5, Matlab 7.0.4 and either PsychToolboz v1.0.5 or v1.0.6)  
> > > > \>  
> > > > \>  
> > > > \> Actual Trial loop:  
> > > > \>  
> > > > \> %%%%%%%%%%%  
> > > > \> for i = 1:length(condOrder)  
> > > > \>  
> > > > \> Screen('FillRect' ,w, black);  
> > > > \> drawFixation( FIX\_COLOR) ;  
> > > > \> Screen('Flip' , w);  
> > > > \>  
> > > > \> if condOrder(i) == 1  
> > > > \> deviant = deviantC1;  
> > > > \> devname = 'deviantC1';  
> > > > \> [...]  
> > > > \> else  
> > > > \> deviant = deviantD3;  
> > > > \> devname = 'deviantD3';  
> > > > \> end  
> > > > \>  
> > > > \> % ----- initialization variables ------  
> > > > \> idx = 1;  
> > > > \> j = 0;  
> > > > \>  
> > > > \> rtime = [];  
> > > > \> keyPresses=[];  
> > > > \> keyTimes =[];  
> > > > \> devTimes = [];  
> > > > \>  
> > > > \> tic;  
> > > > \> iterations = 20;  
> > > > \> numdeviants = 3;  
> > > > \>  
> > > > \> % randomly determine timing of deviants  
> > > > \> tmp = Shuffle(1:5: iterations) ;  
> > > > \> j\_rnd = sort(tmp(1:numdevia nts));  
> > > > \> % ----- end initialization variables ------  
> > > > \>  
> > > > \>  
> > > > \> k = j\_rnd(idx);  
> > > > \>  
> > > > \> TrialTime = GetSecs;  
> > > > \>  
> > > > \>  
> > > > \> while j \<= iterations  
> > > > \> k = j\_rnd(idx);  
> > > > \>  
> > > > \> [keyIsDown,secs, keyCode]= KbCheck();  
> > > > \> keyNumber = getKeyNumber( keyCode); % get numerical value  
> > > > \>  
> > > > \> if ~ isempty(keyNumber)  
> > > > \> keyPresses = [keyPresses keyNumber];  
> > > > \> keyTimes= [keyTimes secs];  
> > > > \> end  
> > > > \>  
> > > > \> if j == k  
> > > > \> devTime = GetSecs;  
> > > > \> devTimes = [devTimes devTime];  
> > > > \>  
> > > > \> sound (deviant,fs) ;  
> > > > \> idx = idx + 1;  
> > > > \>  
> > > > \> if idx \> length(j\_rnd)  
> > > > \> idx = 1;  
> > > > \> end  
> > > > \> else  
> > > > \> sound(triplet, fs);  
> > > > \> end %if  
> > > > \>  
> > > > \> j=j+1;  
> > > > \>  
> > > > \> end %while j  
> > > > \> endtime = clock;  
> > > > \>  
> > > > \>  
> > > > \> %%% sub-function, end of script  
> > > > \> %----------- --------- --------- --------- --------- --------- --   
> > > > \> ----  
> > > > \> function keyNumber = getKeyNumber( keyCode )  
> > > > \> global ESC\_KEY  
> > > > \> keyNumber=[] ;  
> > > > \> for i=1:length(keyCode)  
> > > > \> if keyCode(i)  
> > > > \> keyName=upper( KbName(i) ); %get the key name from the  
> > > > \> keyboard key index  
> > > > \> if strcmp(keyName, ESC\_KEY)  
> > > > \> error('ESCAPE key detected');  
> > > > \> end  
> > > > \> keyNumber=str2num( keyName); %turn the key name into a   
> > > > \> number  
> > > > \> return; %return once the first key pressed is found  
> > > > \> end  
> > > > \> end  
> > > > \> %----------- --------- --------- --------- --------- --------- --   
> > > > \> ----  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \> Post your message to: [psychtoolbox@](mailto:psychtoolbox%40yahoogroups.com) yahoogroups. com  
> > > > \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> > > > \> Denis Pelli, David Brainard, and Allen Ingling.  
> > > > \> [http://psychtoolbox](http://psychtoolbox.org) .org  
> > > > \>  
> > > > \> Yahoo! Groups Links  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>  
> > > > \>
> > > 
> > >   
> > > 
> > >   
> > > 
> > > * * *
> > > Do you Yahoo!?  
> > > Everyone is raving about the [all-new Yahoo! Mail.](http://us.rd.yahoo.com/evt=42297/*http://advision.webevents.yahoo.com/mailbeta)
> 
>   
> 
> * * *
> Get your own [web address for just $1.99/1st yr](https://psychtoolbox.discourse.group/ http://us.rd.yahoo.com/evt=43290/*http://smallbusiness.yahoo.com/domains). We'll help. [Yahoo! Small Business](http://us.rd.yahoo.com/evt=41244/*http://smallbusiness.yahoo.com/).

---

<div class="post-metadata">

**Author:** ![majubma](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@majubma](https://psychtoolbox.discourse.group/u/majubma)\
**Post date:** [August 29, 2006, 9:45pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/6 "2006-08-29T21:45:35Z")

</div>

This is one common situation where it would be very nice for  
psychtoolbox to support the OS's inbuilt notion of event queues. The  
OS automatically records all keypresses and mouse movements (and the  
time they occurred!) in a queue while your program is doing other  
things like updating the display. You can read events out of the queue  
manually, or ask the OS to invoke a callback function in your code  
when a particular kind of event happens. Mac OS 9 and X do it, Windows  
does it, X11 does it, and so on. Java has a platform-independent  
implementation which you might be able to make work with MATLAB.  
  
Almost every software application on your computer works by  
interacting with the event queue -- looping while constantly checking  
your keyboard state is something practically no one has done since the  
DOS days.  
  
Given the quite ordinary requirement to run a simulus display while  
collecting input from the user, I'm sort of baffled that Psychtoolbox  
does not support this mechanism.  
  

> On 8/29/06, Keith Schneider \<[ks@...](mailto:ks@...)\> wrote:  
> \>  
> \>  
> \>  
> \>  
> \>  
> \>  
> \> Hi Virginie,  
> \>  
> \> Let me reiterate that the KbCheck returns the instantaneous state of the  
> \> keyboard, that is, which keys are being pressed at the very moment that you  
> \> call the function. Therefore, within a trial, you should stop checking the  
> \> keyboard after the first time at which the KbCheck function indicates that a  
> \> key is being pressed. If you keep calling the function every few  
> \> milliseconds, the function will report that the key that was pressed is  
> \> still pressed. It takes some time for the key to be pressed and then  
> \> released.  
> \>  
> \> I don't know how your code is structured exactly, but you can also implement  
> \> a check to make sure all the keys are released before continuing to the next  
> \> trial.  
> \>  
> \> keith  
> \>  
> \>  
> \> On Aug 29, 2006, at 4:04 PM, Virginie van Wassenhove wrote:  
> \> Hi Keith,  
> \>  
> \> thanks for the code snippet. I have tried to implement it (within the  
> \> iteration loop) but it is still not stable. It seems that a WaitSecs of 1ms  
> \> is too short -i.e. it does not seem to flush out the previous keypresses -  
> \> and 10 to 100ms either partially reports the key presses, not at all, or too  
> \> many....  
> \> I'll try and work around these things some more  
> \>  
> \> Thanks again!  
> \> Virginie  
> \>  
> \> Keith Schneider \<[ks@...](mailto:ks@...)\> wrote:  
> \>  
> \>  
> \> Dear Virginie,  
> \>  
> \> This seems like the problem then. You will need to have another loop within  
> \> your iteration that continually calls KbCheck, with a waitsecs(0.001) also  
> \> to ensure that the CPU isn't overloaded.  
> \>  
> \> I'd suggest something like this:  
> \>  
> \> exit\_time = GetSecs + iteration\_duration;  
> \> while(GetSecs \< exit\_time)  
> \> [keyisdown, secs, keycode] = KbCheck;  
> \> if keyisdown  
> \> break  
> \> end  
> \> waitsecs(0.001)  
> \> end  
> \>  
> \> This will continually poll your keyboard, breaking out of the loop once a  
> \> key is pressed, or exiting when the specified duration has elapsed. You can  
> \> do other things if you need to within the loop as well.  
> \>  
> \> keith  
> \>  
> \>  
> \> On Aug 29, 2006, at 1:30 PM, Virginie van Wassenhove wrote:  
> \> Dear Keith,  
> \>  
> \> yes this is in fact an issue. I have tried to use the KbCheck within the  
> \> 'if' statement (in addition to within the 'while') or create another 'while'  
> \> statement for the KbCheck before the while pertaining to the iteration. None  
> \> of these two worked.  
> \>  
> \> Each iteration is ~550ms but can vary with stimulus length, no longer than  
> \> 1s -I don't expect to have multiple key presses during that time.  
> \>  
> \> Thanks,  
> \> Virginie  
> \> Keith Schneider \<[ks@...](mailto:ks@...)\> wrote:  
> \>  
> \>  
> \> Dear Vvanwass,  
> \>  
> \> Looking at your script briefly, it looks like you are polling the  
> \> keyboard only once per "iteration". How long is each iteration? You  
> \> should be polling the keyboard every few milliseconds at least.  
> \> Remember, the KbCheck function only tells you whether a key is  
> \> depressed at the instant that the function is called.  
> \>  
> \> keith  
> \>  
> \> On Aug 29, 2006, at 1:01 PM, vvanwass wrote:  
> \>  
> \> \> Hello List,  
> \> \>  
> \> \> I'd like to have responses recorded continuously while various stimuli  
> \> \> are being presented (by recorded I mean (1) the key that was pressed  
> \> \> and (2) the 'reaction time' from trial onset). There is no constraint  
> \> \> as to when the participant is to respond (hence before, during or  
> \> \> after each stimulus is valid). Each trial can be as long as 5 minutes.  
> \> \>  
> \> \> I am putting an excerpt of the script below. This is not currently  
> \> \> doing the job. I have tried a couple different ways but somehow I  
> \> \> never get the full set of responses. (i.e. if I test myself and press  
> \> \> 10 times a key during the period, only 2 of them might be recorded).  
> \> \>  
> \> \> I have tried to change the position of the KbCheck in and out of the  
> \> \> loop, create a timing loop before and including the trial  
> \> \> presentation...I am sure there is a very simple way to do this, but I  
> \> \> can't seem to find it....  
> \> \>  
> \> \> Any help would be most appreciated!! Thanks in advance!  
> \> \>  
> \> \> (System: G5, Matlab 7.0.4 and either PsychToolboz v1.0.5 or v1.0.6)  
> \> \>  
> \> \>  
> \> \> Actual Trial loop:  
> \> \>  
> \> \> %%%%%%%%%%%  
> \> \> for i = 1:length(condOrder)  
> \> \>  
> \> \> Screen('FillRect',w, black);  
> \> \> drawFixation(FIX\_COLOR);  
> \> \> Screen('Flip', w);  
> \> \>  
> \> \> if condOrder(i) == 1  
> \> \> deviant = deviantC1;  
> \> \> devname = 'deviantC1';  
> \> \> [...]  
> \> \> else  
> \> \> deviant = deviantD3;  
> \> \> devname = 'deviantD3';  
> \> \> end  
> \> \>  
> \> \> % ----- initialization variables ------  
> \> \> idx = 1;  
> \> \> j = 0;  
> \> \>  
> \> \> rtime = [];  
> \> \> keyPresses=[];  
> \> \> keyTimes =[];  
> \> \> devTimes = [];  
> \> \>  
> \> \> tic;  
> \> \> iterations = 20;  
> \> \> numdeviants = 3;  
> \> \>  
> \> \> % randomly determine timing of deviants  
> \> \> tmp = Shuffle(1:5:iterations);  
> \> \> j\_rnd = sort(tmp(1:numdeviants));  
> \> \> % ----- end initialization variables ------  
> \> \>  
> \> \>  
> \> \> k = j\_rnd(idx);  
> \> \>  
> \> \> TrialTime = GetSecs;  
> \> \>  
> \> \>  
> \> \> while j \<= iterations  
> \> \> k = j\_rnd(idx);  
> \> \>  
> \> \> [keyIsDown,secs,keyCode]= KbCheck();  
> \> \> keyNumber = getKeyNumber(keyCode); % get numerical value  
> \> \>  
> \> \> if ~ isempty(keyNumber)  
> \> \> keyPresses = [keyPresses keyNumber];  
> \> \> keyTimes= [keyTimes secs];  
> \> \> end  
> \> \>  
> \> \> if j == k  
> \> \> devTime = GetSecs;  
> \> \> devTimes = [devTimes devTime];  
> \> \>  
> \> \> sound (deviant,fs);  
> \> \> idx = idx + 1;  
> \> \>  
> \> \> if idx \> length(j\_rnd)  
> \> \> idx = 1;  
> \> \> end  
> \> \> else  
> \> \> sound(triplet,fs);  
> \> \> end %if  
> \> \>  
> \> \> j=j+1;  
> \> \>  
> \> \> end %while j  
> \> \> endtime = clock;  
> \> \>  
> \> \>  
> \> \> %%% sub-function, end of script  
> \> \> %----------------------------------------------------------  
> \> \> ----  
> \> \> function keyNumber = getKeyNumber( keyCode )  
> \> \> global ESC\_KEY  
> \> \> keyNumber=[];  
> \> \> for i=1:length(keyCode)  
> \> \> if keyCode(i)  
> \> \> keyName=upper(KbName(i)); %get the key name from the  
> \> \> keyboard key index  
> \> \> if strcmp(keyName, ESC\_KEY)  
> \> \> error('ESCAPE key detected');  
> \> \> end  
> \> \> keyNumber=str2num(keyName); %turn the key name into a  
> \> \> number  
> \> \> return; %return once the first key pressed is found  
> \> \> end  
> \> \> end  
> \> \> %----------------------------------------------------------  
> \> \> ----  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \> 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  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \> \>  
> \>  
> \>  
> \>  
> \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \> Do you Yahoo!?  
> \> Everyone is raving about the all-new Yahoo! Mail.  
> \>  
> \>  
> \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \> Get your own web address for just $1.99/1st yr. We'll help. Yahoo! Small  
> \> Business.  
> \>  
> \>  
> \>

---

<div class="post-metadata">

**Author:** ![vvanwass](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@vvanwass](https://psychtoolbox.discourse.group/u/vvanwass)\
**Post date:** [September 5, 2006, 9:55pm UTC](https://psychtoolbox.discourse.group/t/continuous-registering-responses-with-kbcheck/771/7 "2006-09-05T21:55:39Z")

</div>

Hi   
  
 this is to give an update on this issue...&nbsp; CharAvail and GetChar in v1.0.6 beta works very well for the purpose of recording key presses at anytime/continuously during the script.  
  
 Thanks all for your help/comments on this and double-thanks to Chris for the very fast updates!  
  
 -Virginie  
  
**_Christopher Broussard \<chrg@...\>_** wrote:

> > Hi Virginie  
> > &nbsp;&nbsp;&nbsp; I'm currently reworking the implementation of GetChar and CharAvail, which may be what you want to use.&nbsp; It essentially buffers all keypresses in the background and let's you pull them out at your leisure.&nbsp; KbCheck is quite fast, but unfortunately, as you have had happen, can miss keypresses if you don't time it right.&nbsp; GetChar&nbsp; also records when the key was pressed so you can keep track of that information.&nbsp; I'm finishing up the final touches on it right now and hopefully I can get it into the beta today or tomorrow.  
> >   
> > &nbsp;&nbsp;&nbsp; -Christopher Broussard-  
> >   
> > Virginie van Wassenhove wrote: 
> > 
> > > Hi Keith,   
> > >   
> > > thanks for the code snippet. I have tried to implement it (within the iteration loop) but it is still not stable. It seems that a WaitSecs of 1ms is too short -i.e. it does not seem to flush out the previous keypresses - and 10 to 100ms either partially reports the key presses, not at all, or too many....  
> > > I'll try and work around these things some more  
> > >   
> > > Thanks again!  
> > > Virginie  
> > >   
> > > **_Keith Schneider \<[ks@...](mailto:ks@...). edu\>_** wrote:
> > > 
> > > > Dear Virginie,
> > > > 
> > > >   
> > > > 
> > > > This seems like the problem then.&nbsp; You will need to have another loop within your iteration that continually calls KbCheck, with a waitsecs(0.001) also to ensure that the CPU isn't overloaded.
> > > > 
> > > >   
> > > > 
> > > > I'd suggest something like this:
> > > > 
> > > >   
> > > > 
> > > > exit\_time = GetSecs + iteration\_duration;
> > > > 
> > > > while(GetSecs \< exit\_time)
> > > > 
> > > > &nbsp; &nbsp;[keyisdown, secs, keycode] = KbCheck;
> > > > 
> > > > &nbsp; &nbsp;if keyisdown
> > > > 
> > > > &nbsp;&nbsp; &nbsp;break
> > > > 
> > > > &nbsp; end
> > > > 
> > > > &nbsp; waitsecs(0.001)
> > > > 
> > > > end
> > > > 
> > > >   
> > > > 
> > > > This will continually poll your keyboard, breaking out of the loop once a key is pressed, or exiting when the specified duration has elapsed.&nbsp; You can do other things if you need to within the loop as well.
> > > > 
> > > >   
> > > > 
> > > > keith
> > > > 
> > > >   
> > > > 
> > > > On Aug 29, 2006, at 1:30 PM, Virginie van Wassenhove wrote:
> > > >   
> > > > 
> > > > > Dear Keith,  
> > > > >   
> > > > > yes this is in fact an issue. I have tried to use the KbCheck within the 'if' statement (in addition to within the 'while') or create ano! ther 'wh ile' statement for the KbCheck before the while pertaining to the iteration. None of these two worked.   
> > > > >   
> > > > > Each iteration is ~550ms but can vary with stimulus length, no longer than 1s -I don't expect to have multiple key presses during that time.  
> > > > >   
> > > > > Thanks,   
> > > > > Virginie  
> > > > > **_Keith Schneider \<[ks@.... edu](mailto:ks@...)\>_** wrote: 
> > > > > 
> > > > > > Dear Vvanwass,  
> > > > > >   
> > > > > > Looking at your script briefly, it looks like you are polling the   
> > > > > > keyboard only once per "iteration". How long is each iteration? You   
> > > > > > should be polling the keyboard every few milliseconds at least.   
> > > > > > Remember, the KbCheck function only tells you whether a key is   
> > > > > > depressed at the instant that the function is called.  
> > > > > >   
> > > > > > keith  
> > > > > >   
> > > > > > On Aug 29, 2006, at 1:01 PM, vvanwass wrote:  
> > > > > >   
> > > > > > \> Hello List,  
> > > > > > \>  
> > > > > > \> I'd like to have responses recorded continuously while various stimuli  
> > > > > > \> are being presented (by recorded I mean (1) the key that was pressed  
> > > > > > \> and (2) the 'reaction time' from trial onset). There is no constraint  
> > > > > > \> as to when the participant is to respond (hence before, during or  
> > > > > > \> after each stimulus is valid). Each trial can be as long as 5 minutes.  
> > > > > > \>  
> > > > > > \> I am putting an excerpt of the script below. This is not currently  
> > > > > > \> doing the job. I have tried a couple different ways but somehow I  
> > > > > > \> never get the full set of responses. (i.e. if I test myself and press  
> > > > > > \> 10 times a key during the period, only 2 of them might be recorded).  
> > > > > > \>  
> > > > > > \> I have tried to change the position of the KbCheck in and out of the  
> > > > > > \> loop, create a timing loop before and including the trial  
> > > > > > \> presentation. ..I am sure there is a very simple way to do this, but I  
> > > > > > \> can't seem to find it....  
> > > > > > \>  
> > > > > > \> Any help would be most appreciated! ! Thanks in advance!  
> > > > > > \>  
> > > > > > \> (System: G5, Matlab 7.0.4 and either PsychToolboz v1.0.5 or v1.0.6)  
> > > > > > \>  
> > > > > > \>  
> > > > > > \> Actual Trial loop:  
> > > > > > \>  
> > > > > > \> %%%%%%%%%%%  
> > > > > > \> for i = 1:length(condOrder)  
> > > > > > \>  
> > > > > > \> Screen('FillRect' ,w, black);  
> > > > > > \> drawFixation( FIX\_COLOR) ;  
> > > > > > \> Screen('Flip' , w);  
> > > > > > \>  
> > > > > > \> if condOrder(i) == 1  
> > > > > > \> deviant = deviantC1;  
> > > > > > \> devname = 'deviantC1';  
> > > > > > \> [...]  
> > > > > > \> else  
> > > > > > \> deviant = deviantD3;  
> > > > > > \> devname = 'deviantD3';  
> > > > > > \> end  
> > > > > > \>  
> > > > > > \> % ----- initialization variables ------  
> > > > > > \> idx = 1;  
> > > > > > \> j = 0;  
> > > > > > \>  
> > > > > > \> rtime = [];  
> > > > > > \> keyPresses=[];  
> > > > > > \> keyTimes =[];  
> > > > > > \> devTimes = [];  
> > > > > > \>  
> > > > > > \> tic;  
> > > > > > \> iterations = 20;  
> > > > > > \> numdeviants = 3;  
> > > > > > \>  
> > > > > > \> % randomly determine timing of deviants  
> > > > > > \> tmp = Shuffle(1:5: iterations) ;  
> > > > > > \> j\_rnd = sort(tmp(1:numdevia nts));  
> > > > > > \> % ----- end initialization variables ------  
> > > > > > \>  
> > > > > > \>  
> > > > > > \> k = j\_rnd(idx);  
> > > > > > \>  
> > > > > > \> TrialTime = GetSecs;  
> > > > > > \>  
> > > > > > \>  
> > > > > > \> while j \<= iterations  
> > > > > > \> k = j\_rnd(idx);  
> > > > > > \>  
> > > > > > \> [keyIsDown,secs, keyCode]= KbCheck();  
> > > > > > \> keyNumber = getKeyNumber( keyCode); % get numerical value  
> > > > > > \>  
> > > > > > \> if ~ isempty(keyNumber)  
> > > > > > \> keyPresses = [keyPresses keyNumber];  
> > > > > > \> keyTimes= [keyTimes secs];  
> > > > > > \> end  
> > > > > > \>  
> > > > > > \> if j == k  
> > > > > > \> devTime = GetSecs;  
> > > > > > \> devTimes = [devTimes devTime];  
> > > > > > \>  
> > > > > > \> sound (deviant,fs) ;  
> > > > > > \> idx = idx + 1;  
> > > > > > \>  
> > > > > > \> if idx \> length(j\_rnd)  
> > > > > > \> idx = 1;  
> > > > > > \> end  
> > > > > > \> else  
> > > > > > \> sound(triplet, fs);  
> > > > > > \> end %if  
> > > > > > \>  
> > > > > > \> j=j+1;  
> > > > > > \>  
> > > > > > \> end %while j  
> > > > > > \> endtime = clock;  
> > > > > > \>  
> > > > > > \>  
> > > > > > \> %%% sub-function, end of script  
> > > > > > \> %----------- --------- --------- --------- --------- --------- --   
> > > > > > \> ----  
> > > > > > \> function keyNumber = getKeyNumber( keyCode )  
> > > > > > \> global ESC\_KEY  
> > > > > > \> keyNumber=[] ;  
> > > > > > \> for i=1:length(keyCode)  
> > > > > > \> if keyCode(i)  
> > > > > > \> keyName=upper( KbName(i) ); %get the key name from the  
> > > > > > \> keyboard key index  
> > > > > > \> if strcmp(keyName, ESC\_KEY)  
> > > > > > \> error('ESCAPE key detected');  
> > > > > > \> end  
> > > > > > \> keyNumber=str2num( keyName); %turn the key name into a   
> > > > > > \> number  
> > > > > > \> return; %return once the first key pressed is found  
> > > > > > \> end  
> > > > > > \> end  
> > > > > > \> %----------- --------- --------- --------- --------- --------- --   
> > > > > > \> ----  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \> Post your message to: [psychtoolbox@](mailto:psychtoolbox%40yahoogroups.com) yahoogroups. com  
> > > > > > \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> > > > > > \> Denis Pelli, David Brainard, and Allen Ingling.  
> > > > > > \> [http://psychtoolbox](http://psychtoolbox.org) .org  
> > > > > > \>  
> > > > > > \> Yahoo! Groups Links  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>  
> > > > > > \>
> > > > > 
> > > > >   
> > > > > 
> > > > >    
> > > > > 
> > > > > * * *
> > > > > Do you Yahoo!?  
> > > > > Everyone is raving about the [all-new Yahoo! Mail.](http://us.rd.yahoo.com/evt=42297/*http://advision.webevents.yahoo.com/mailbeta)
> > > 
> > >   
> > > 
> > >  
> > > 
> > > * * *
> > > Get your own web address for just $1.99/1st yr. We'll help. [Yahoo! Small Business](http://us.rd.yahoo.com/evt=41244/*http://smallbusiness.yahoo.com/).
> 
>   
> 
> * * *
> Stay in the know. Pulse on the new Yahoo.com. [Check it out.](http://us.rd.yahoo.com/evt=42974/*http://www.yahoo.com/preview)
