# PsychHID KbQueueGetEvent not working properly

**URL:** <https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757>\
**Category:** Programming Help\
**Tags:** windows, experiment-design, timing\
**Created:** [December 20, 2022, 4:41pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757 "2022-12-20T16:41:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsebas24](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jsebas24](https://psychtoolbox.discourse.group/u/jsebas24)\
**Post date:** [December 20, 2022, 4:41pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/1 "2022-12-20T16:41:36Z")

</div>

Hello everyone!  
I’m trying to program a Matlab routine on Windows, the idea is to press a mouse click following an audio signal. I need hi precision, so after reading the documentation I decided to use PsychHID functions.  
In the first place, I’m trying to capture the event mouse click during 5 seconds and get time, then accumulate it in a vector ([times]) but it doesn’t work, sometimes it gets more events than I clicked and the time between clicks is always the same.  
Did somebody try this before? I will be grateful for any help  
Here is my code:

```matlab
devices=PsychHID('Devices',1 );
Priority(MaxPriority(0));
PsychHID('KbQueueCreate',1) ;
PsychHID('KbQueueStart',1) ;
times=[];
tend = GetSecs + 5;
while GetSecs < tend
[event,navail] = PsychHID('KbQueueGetEvent' , 1,5)
times=[times ,event.Time];
end

PsychHID('KbQueueStop' ,1)
PsychHID('KbQueueRelease' ,1)
Priority(0);

```

---

<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:** [December 20, 2022, 4:56pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/2 "2022-12-20T16:56:30Z")

</div>

Start by using GetMouseIndices() to get the device id of your mouse, and using that instead of 1 for all the PsychHID calls.

Also, you do not need to read the event queue in the loop, you can read it once after the 5 seconds.

---

<div class="post-metadata">

**Author:** ![jsebas24](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jsebas24](https://psychtoolbox.discourse.group/u/jsebas24)\
**Post date:** [December 20, 2022, 5:17pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/3 "2022-12-20T17:17:06Z")

</div>

Hi dcniecho,  
Thank you for your answer.!You are right, better to use GetMouseIndices().  
My question is: if I do not use a loop how can I save the event time? reading the documentation the function will return only the last event.  
I need to capture all of them and calculate the intervals between them.

---

<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:** [December 21, 2022, 3:26am UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/4 "2022-12-21T03:26:01Z")

</div>

Don’t use PsychHID() directly, but the KbQueueCreate/Start/…/ functions. For everything else KbQueueDemo.m.

---

<div class="post-metadata">

**Author:** ![jsebas24](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jsebas24](https://psychtoolbox.discourse.group/u/jsebas24)\
**Post date:** [January 6, 2023, 2:28pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/5 "2023-01-06T14:28:25Z")

</div>

Thank’s @mariokleiner  
Here is a simple solution that solves my problem initially.

devices=PsychHID(‘Devices’,1 );  
mouseIndex=GetMouseIndices();  
Priority(MaxPriority(0));  
PsychHID(‘KbQueueCreate’,mouseIndex) ;  
PsychHID(‘KbQueueStart’,mouseIndex) ;

clickTime=;  
tend = GetSecs + 5;  
pulses=0;

while GetSecs\< tend

[pressed, firstPress, firstRelease, lastPress, lastRelease] = KbQueueCheck(mouseIndex);  
if pressed==1  
pulses=pulses+1;  
clickTime=[clickTime lastPress(1,1)];  
[navail] = PsychHID(‘KbQueueFlush’, mouseIndex);  
end  
end

interv=diff(clickTime)  
pulses  
PsychHID(‘KbQueueStop’ ,mouseIndex)  
PsychHID(‘KbQueueRelease’ ,mouseIndex)  
Priority(0);

---

<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:** [January 6, 2023, 3:51pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/6 "2023-01-06T15:51:34Z")

</div>

As mario said, don’t use PsychHID directly. Also, read the queue once like i said:

```matlab
devices=PsychHID('Devices',1 );
mouseIndex=GetMouseIndices();
KbQueueCreate(mouseIndex);
KbQueueStart(mouseIndex);

pause(5)

% now get what happened during those 5 s
events = [];
nremain = inf;
while ~~nremain
[e,nremain] = KbEventGet(mouseIndex);
events = [events e];
end

KbQueueStop(mouseIndex);
KbQueueRelease(mouseIndex);

% see how many clicks and inter click intervals

mouseDowns = [events.Pressed]==1;
pulses = sum(mouseDowns)
interv = diff([events(mouseDowns).Time])

```

---

<div class="post-metadata">

**Author:** ![jsebas24](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jsebas24](https://psychtoolbox.discourse.group/u/jsebas24)\
**Post date:** [January 6, 2023, 7:13pm UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/7 "2023-01-06T19:13:54Z")

</div>

Thank you for very much your help @dcnieho. Just a question…  
Why using PsychHID directly is not appropriated?

---

<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:** [January 7, 2023, 12:14am UTC](https://psychtoolbox.discourse.group/t/psychhid-kbqueuegetevent-not-working-properly/4757/8 "2023-01-07T00:14:38Z")

</div>

They’re not meant to be. The KbQueue\* wrapper matlab files take care of some input handling and such to make sure things operate correctly. That how they were designed by Mario, so listen to what he says there.
