# KbQueueXXX functions in Ubuntu

**URL:** <https://psychtoolbox.discourse.group/t/kbqueuexxx-functions-in-ubuntu/3825>\
**Category:** Programming Help\
**Tags:** linux\
**Created:** [April 19, 2021, 9:17pm UTC](https://psychtoolbox.discourse.group/t/kbqueuexxx-functions-in-ubuntu/3825 "2021-04-19T21:17:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yu-hsin](https://avatars.discourse-cdn.com/v4/letter/y/c5a1d2/32.png) [@yu-hsin](https://psychtoolbox.discourse.group/u/yu-hsin)\
**Post date:** [April 19, 2021, 9:17pm UTC](https://psychtoolbox.discourse.group/t/kbqueuexxx-functions-in-ubuntu/3825/1 "2021-04-19T21:17:38Z")

</div>

Dear members

I am having a very difficult time using KbQueueXXX functions (i.e., KbQueueCreate, KbQueueStart, KbQueueCheck, KbQueueRelease) to record key presses, kept bumping into errors while following some pretty old step-by-step instructions (e.g., Source One and Two listed at the very end of this post).

I tried to read through the psychtoolbox documentations but still confused with the information presented.  
Some very basic questions such as e.g., should I call  
`PsychHID('KbQueueStart', device) ` or `KbQueueStart(device)` … to look for keypresses?

I was also wondering whether there is any working demo scripts or more recent sources/ tutorials that I can learn from. Thanks in advance.

My script:

```
%%create the Queue device and specify the allowed keys 
device =7; 
keys=[45 46];
keylist=zeros(1,256);
keylist(keys)=1;
KbQueueCreate(device,keylist);

    ...
        
KbQueueStart(device); % look for key presses within a specified time limit
    while TimeOut == 0
       [pressed, firstpress] = KbQueueCheck(device); %check response 
       if pressed
           if firstPress(45)>0
               ...
                TimeOut = 1;
                break

           elseif firstPress(46)>0
               ...
                TimeOut = 1;
                break
           end
       elseif TimeInLoop > 10
               ...
                TimeOut = 1;
                break
       end
    end
KbQueueRelease(device);

```

Error msg presented :

```
Error using PsychHID
Usage:

[keyIsDown, firstKeyPressTimes, firstKeyReleaseTimes, lastKeyPressTimes, lastKeyReleaseTimes]=PsychHID('KbQueueCheck' [, deviceIndex])

Error in KbQueueCheck (line 61)
  [pressed, firstPress, firstRelease, lastPress, lastRelease] = PsychHID('KbQueueCheck', deviceIndex);

Error in myscript (line 465)
           [pressed, firstpress] = KbQueueCheck(device); %check response

```

**References**

Source One  
[http://cbs.fas.harvard.edu/science/core-facilities/neuroimaging/information-investigators/matlabfaq](http://cbs.fas.harvard.edu/science/core-facilities/neuroimaging/information-investigators/matlabfaq)  
Source Two  
ftp://ftp.tuebingen.mpg.de/pub/pub\_dahl/stmdev10\_D/Matlab6/Toolboxes/Psychtoolbox/PsychDocumentation/KbQueue.html

the principle seemed to be

1. KbQueueCreate: create a list of keys
2. KbQueueStart: start “waiting” for keypresses
3. put KbQueueCheck: put this in a while loop to look for specific key presses
4. KbQueueRelease : end the “wait”

**Software environment**  
ubuntu 20.04.1 LTS  
matlab 2019b  
psychtoolbox 3.0.17

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [April 22, 2021, 2:22am UTC](https://psychtoolbox.discourse.group/t/kbqueuexxx-functions-in-ubuntu/3825/2 "2021-04-22T02:22:22Z")

</div>

Those are links to random websites that are not maintained or vetted for correctness by us.

`KbQueueDemo` shows how to use keyboard queues. Your code looks in principle ok, but my first thought would be where does that magic device = 7 come from? Those numbers can change across reboots, if input devices are unplugged/replugged/plugged in etc., so a fixed number might be the wrong fixed number at any given time. `help GetKeyboardIndices`.

-mario
