# How to get mouse and keyboard response on the same trial?

**URL:** <https://psychtoolbox.discourse.group/t/how-to-get-mouse-and-keyboard-response-on-the-same-trial/3092>\
**Category:** Programming Help\
**Tags:** windows, experiment-design\
**Created:** [March 3, 2020, 10:51am UTC](https://psychtoolbox.discourse.group/t/how-to-get-mouse-and-keyboard-response-on-the-same-trial/3092 "2020-03-03T10:51:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RAM](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ram/32/46_2.png) [@RAM](https://psychtoolbox.discourse.group/u/RAM)\
**Post date:** [March 3, 2020, 10:51am UTC](https://psychtoolbox.discourse.group/t/how-to-get-mouse-and-keyboard-response-on-the-same-trial/3092/1 "2020-03-03T10:51:09Z")

</div>

I would like to ask the user to respond to a question with a key press and then click the button below to move to the next question. In my code the key response is registering whether its left or right mouse. it would be of great help if someone could point out what is wrong?

> ```
> > [keyIsDown1, RTkeyCode1] = KbCheck;
> > Q_1=find(RTkeyCode1);
> > EUR_rating=KbName(Q_1);
> > rating.EUR=EUR_rating;
> > timer1_end=0;
> > [x1,y1,buttons1]=GetMouse(window);
> >                      
> > if IsInRect(x1,y1,rect58)
> > Screen('FrameRect', window , [1 0 0], rect58,5)
> > if any(buttons1)
> > timer1_end=GetSecs;
> > Q1=0;
> > end
> > end
> 
> ```

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [March 4, 2020, 5:34am UTC](https://psychtoolbox.discourse.group/t/how-to-get-mouse-and-keyboard-response-on-the-same-trial/3092/2 "2020-03-04T05:34:45Z")

</div>

Your question is unclear to me, but you need to run your code depending on if the user actually pressed the button, so you need to:

```matlab
noResponse = true; %determine when to leave while loop, could also use break directly
while noResponse
    [keyDown, keyCode] = KbCheck(-1);
    if keyDown
       % the rest of your logic should be here
       if any(buttons); noResponse = false; end
    end
    WaitSecs('YieldSecs', 0.01); %throttle the loop a bit
end

```

Or use `KbWait` that saves you having to run the outer loop…

---

<div class="post-metadata">

**Author:** ![RAM](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ram/32/46_2.png) [@RAM](https://psychtoolbox.discourse.group/u/RAM)\
**Post date:** [March 4, 2020, 6:30am UTC](https://psychtoolbox.discourse.group/t/how-to-get-mouse-and-keyboard-response-on-the-same-trial/3092/3 "2020-03-04T06:30:18Z")

</div>

Thanks for the response. I didn’t mean that. Let me rephrase myself, I’m presenting a question which asks them to rate something and the user gives their answer by giving a key response, in this case they rate it by a number. after giving the response, the user need to click on a rect on the same screen,right below the question to move to the next question.
