# How to get keyboard response during the moving stimulus?

**URL:** <https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102>\
**Category:** Programming Help\
**Created:** [November 8, 2021, 9:41am UTC](https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102 "2021-11-08T09:41:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangguretong](https://avatars.discourse-cdn.com/v4/letter/y/e8c25b/32.png) [@yangguretong](https://psychtoolbox.discourse.group/u/yangguretong)\
**Post date:** [November 8, 2021, 9:41am UTC](https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102/1 "2021-11-08T09:41:39Z")

</div>

Hello, everyone.  
I am struggling to code a problem to record participants’ responses during the presentation of a moving stimulus (random dot kinematograms, RDK ). Since the RDK is a frame by frame changing paradigm, I put KbCheck into this loop. However, I can’t record the response. I have struggled with it all day with no solutions. Can anyone give me any advice?  
Thank you very much

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [November 8, 2021, 10:09am UTC](https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102/2 "2021-11-08T10:09:52Z")

</div>

Provide information about your OS, matlab version and PTB version (you are fully up to date)? Do the keyboard demos work for you? If so, provide _minimal_ code that doesn’t work (not your whole program)

---

<div class="post-metadata">

**Author:** ![yangguretong](https://avatars.discourse-cdn.com/v4/letter/y/e8c25b/32.png) [@yangguretong](https://psychtoolbox.discourse.group/u/yangguretong)\
**Post date:** [November 9, 2021, 1:38am UTC](https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102/3 "2021-11-09T01:38:10Z")

</div>

Thank you for your reply.  
My OS is Catalina (version10.15.7), Matlab is 2021a, and PTB is PTB-3. KbDemo works fine with my computer.  
My code is:

```matlab
keyIsDown = 0;
for frameNum=1:nFrames % nFrames = 60, that is, RDK lasts one seconds 
       DrawRDK in one frame;
       Flip;
        [keyIsDown, sec, keyCode] = KbCheck; 
        if keyIsDown
            Record the keyCode and sec in my structure;
            keyIsDown = 0;
       end
end % end of for loop

```

It turns out the RDK stimulus works fine, but I can’t get any response even I press a key. When I debug it, it works if I keep pressing a key, so I can’t find any problems right now.  
Thank you very much.

---

<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:** [November 9, 2021, 10:42am UTC](https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102/4 "2021-11-09T10:42:49Z")

</div>

So if you put a `disp('keyIsDown was TRUE!!!')` inside your `if keyIsDown` loop do you ever see this message printed to the MATLAB window?

Try to pass `-1` as a device number (`KbCheck(-1)`) to make sure all keyboards are scanned.

And finally check that you don’t have a problem with ‘stuck keys’, see `doc Kbcheck` for more details…

---

<div class="post-metadata">

**Author:** ![yangguretong](https://avatars.discourse-cdn.com/v4/letter/y/e8c25b/32.png) [@yangguretong](https://psychtoolbox.discourse.group/u/yangguretong)\
**Post date:** [November 10, 2021, 12:26am UTC](https://psychtoolbox.discourse.group/t/how-to-get-keyboard-response-during-the-moving-stimulus/4102/5 "2021-11-10T00:26:00Z")

</div>

Dear Ian Max Andolina,  
Thank you so much !!!  
I took your advice and found my problem finally. It turned out my code in this “for loop” was correct. A minor problem far away from this loop existed, leading to no message when I put `disp('keyIsDown was TRUE!!!')`. When I resolved that problem and used KbCheck(-1), everything went well.  
Thank you so much! I have been stuck for two days because of this minor problem. You saved my day!  
Best Wishes.
