PsychHID KbQueueGetEvent not working properly

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:

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);

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.

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.

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

1 Like

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);

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

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])

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

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.