# problem with stimulus presentation and key recording

**URL:** <https://psychtoolbox.discourse.group/t/problem-with-stimulus-presentation-and-key-recording/1434>\
**Category:** Uncategorized\
**Created:** [October 10, 2014, 9:02pm UTC](https://psychtoolbox.discourse.group/t/problem-with-stimulus-presentation-and-key-recording/1434 "2014-10-10T21:02:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cohen.joe28](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@cohen.joe28](https://psychtoolbox.discourse.group/u/cohen.joe28)\
**Post date:** [October 10, 2014, 9:02pm UTC](https://psychtoolbox.discourse.group/t/problem-with-stimulus-presentation-and-key-recording/1434/1 "2014-10-10T21:02:39Z")

</div>

Hi everyone,  
  
Thanks for the help provided through this forum. This is my problem: I have something like this

%I generate two stimuli and a fixation cross and I present them simultaneously

% here is the first stimulus

&nbsp;Screen('FillOval', w, [255 255 255], Stimulus1);

% here is my fixation cross

[X,Y] = RectCenter(scherm);

FixCross = [X-0.5,Y-20,X+0.5,Y+20;X-20,Y-0.5,X+20,Y+0.5];

Screen('FillRect', w, [255 0 0], FixCross');

%here is the second stimulus

Screen('FillOval', w, stimColor,sizesS);

% now the two stimuli are presented on the screen one on the left and one on the right with the fixation cross in the middle

onset = Screen('Flip',w,onset + (waitframes - 0.5) \*1\* ifi);

% The subjects has to make a decision between the two stimuli by pressing left or right and I record the key they press and reaction times

tStart = GetSecs;

&nbsp; &nbsp; respToBeMade = true;

&nbsp; &nbsp; while respToBeMade

&nbsp; &nbsp; &nbsp; &nbsp; [keyIsDown,secs(i), keyCode(i)] = KbCheck;

&nbsp; &nbsp; &nbsp; &nbsp; if keyCode(escapeKey)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCursor;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sca;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return

&nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(leftKey)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; respToBeMade = false;

&nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(rightKey)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = 0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; respToBeMade = false;

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; end

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

% Here are the reaction times

&nbsp; &nbsp; tEnd = GetSecs;

&nbsp; &nbsp; rt(j) = tEnd - tStart;

Now, my problem is that if I run the script, after I press a button for a response in the first trial I am presented with a trail that remains on the screen for few msecs and then another trial during which I can give a response. And then again, one trial flips fastly and a second one 'remains on the screen' so in the end I can collect only reaction times for half of the trials. Of course I want to collect reaction times for all trials and not only for one out of two! One thing I noticed is that if I change &nbsp;the following code

% The subjects has to make a decision between the two stimuli by pressing left or right and I record the key they press and reaction times

tStart = GetSecs;

&nbsp; &nbsp; respToBeMade = true;

&nbsp; &nbsp; while respToBeMade

&nbsp; &nbsp; &nbsp; &nbsp; [keyIsDown,secs(i), keyCode(i)] = KbCheck;

&nbsp; &nbsp; &nbsp; &nbsp; if keyCode(escapeKey)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCursor;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sca;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return

&nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(leftKey)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; respToBeMade = false;

&nbsp; &nbsp; &nbsp; &nbsp; elseif keyCode(rightKey)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = 0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; respToBeMade = false;

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; end

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

% Here are the reaction times

&nbsp; &nbsp; tEnd = GetSecs;

&nbsp; &nbsp; rt(j) = tEnd - tStart;

  

simply with a more basic KbStrokeWait each trial is presented and I have to press a button to move to the other trial (but of course&nbsp;KbStrokeWait&nbsp;is not what I need to collect reaction times so I haven't solved my problem).

  

Any suggestion on how to solve this problem?

I hope my question is well formulated and clear.

Thank you in advance,

Joe

---

<div class="post-metadata">

**Author:** ![xeos](https://avatars.discourse-cdn.com/v4/letter/x/ecae2f/32.png) [@xeos](https://psychtoolbox.discourse.group/u/xeos)\
**Post date:** [October 10, 2014, 10:18pm UTC](https://psychtoolbox.discourse.group/t/problem-with-stimulus-presentation-and-key-recording/1434/2 "2014-10-10T22:18:17Z")

</div>

Your code is a little long to read, but sounds like the first key press is getting picked up more than once. kbstrokewait waits for the key to be released before continuing, and you need to, too. Or, add a fixed ISI between trials.

  

> On Fri, Oct 10, 2014 at 2:02 PM, [cohen.joe28@...](mailto:cohen.joe28@...) [PSYCHTOOLBOX] \<[PSYCHTOOLBOX@yahoogroups.com](mailto:PSYCHTOOLBOX@yahoogroups.com)\> wrote:  
> 
> > Hi everyone,  
> >   
> > Thanks for the help provided through this forum. This is my problem: I have something like this
> > 
> > %I generate two stimuli and a fixation cross and I present them simultaneously
> > 
> > % here is the first stimulus
> > 
> > Screen('FillOval', w, [255 255 255], Stimulus1);
> > 
> > % here is my fixation cross
> > 
> > [X,Y] = RectCenter(scherm);
> > 
> > FixCross = [X-0.5,Y-20,X+0.5,Y+20;X-20,Y-0.5,X+20,Y+0.5];
> > 
> > Screen('FillRect', w, [255 0 0], FixCross');
> > 
> > %here is the second stimulus
> > 
> > Screen('FillOval', w, stimColor,sizesS);
> > 
> > % now the two stimuli are presented on the screen one on the left and one on the right with the fixation cross in the middle
> > 
> > onset = Screen('Flip',w,onset + (waitframes - 0.5) \*1\* ifi);
> > 
> > % The subjects has to make a decision between the two stimuli by pressing left or right and I record the key they press and reaction times
> > 
> > tStart = GetSecs;
> > 
> > respToBeMade = true;
> > 
> > while respToBeMade
> > 
> > [keyIsDown,secs(i), keyCode(i)] = KbCheck;
> > 
> > if keyCode(escapeKey)
> > 
> > ShowCursor;
> > 
> > sca;
> > 
> > return
> > 
> > elseif keyCode(leftKey)
> > 
> > response = 1;
> > 
> > respToBeMade = false;
> > 
> > elseif keyCode(rightKey)
> > 
> > response = 0;
> > 
> > respToBeMade = false;
> > 
> > end
> > 
> > end
> > 
> > % Here are the reaction times
> > 
> > tEnd = GetSecs;
> > 
> > rt(j) = tEnd - tStart;
> > 
> > Now, my problem is that if I run the script, after I press a button for a response in the first trial I am presented with a trail that remains on the screen for few msecs and then another trial during which I can give a response. And then again, one trial flips fastly and a second one 'remains on the screen' so in the end I can collect only reaction times for half of the trials. Of course I want to collect reaction times for all trials and not only for one out of two! One thing I noticed is that if I change the following code
> > 
> > % The subjects has to make a decision between the two stimuli by pressing left or right and I record the key they press and reaction times
> > 
> > tStart = GetSecs;
> > 
> > respToBeMade = true;
> > 
> > while respToBeMade
> > 
> > [keyIsDown,secs(i), keyCode(i)] = KbCheck;
> > 
> > if keyCode(escapeKey)
> > 
> > ShowCursor;
> > 
> > sca;
> > 
> > return
> > 
> > elseif keyCode(leftKey)
> > 
> > response = 1;
> > 
> > respToBeMade = false;
> > 
> > elseif keyCode(rightKey)
> > 
> > response = 0;
> > 
> > respToBeMade = false;
> > 
> > end
> > 
> > end
> > 
> > % Here are the reaction times
> > 
> > tEnd = GetSecs;
> > 
> > rt(j) = tEnd - tStart;
> > 
> >   
> > 
> > simply with a more basic KbStrokeWait each trial is presented and I have to press a button to move to the other trial (but of course KbStrokeWait is not what I need to collect reaction times so I haven't solved my problem).
> > 
> >   
> > 
> > Any suggestion on how to solve this problem?
> > 
> > I hope my question is well formulated and clear.
> > 
> > Thank you in advance,
> > 
> > Joe

---

<div class="post-metadata">

**Author:** ![cohen.joe28](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@cohen.joe28](https://psychtoolbox.discourse.group/u/cohen.joe28)\
**Post date:** [October 13, 2014, 2:58pm UTC](https://psychtoolbox.discourse.group/t/problem-with-stimulus-presentation-and-key-recording/1434/3 "2014-10-13T14:58:50Z")

</div>

Thank you all for the very useful and helpful discussion! My script looks and works definitely better now,
  

Kind regards,

  

Joe
