Better wait for back tick: KbCheck or GetChar?

When a 1-millisecond-duration TTL pulse is sent to the BNC connector of our
Rowland USB fMRI hand pucks, a back tick (`) keyboard character is generated.
And when that happens, we would like our Matlab program to react as quickly as
possible.

I am trying to figure out which of the following is the better way to have my
Matlab code pause, wait for that TTL pulse, and be the fastest off the bat. I
have read what is out there on the differences between KbCheck and GetChar, but
in this specific application, might GetChar be the better choice? That is, is
it possible that the 1-millisecond-duration TTL pulse might be missed between
successive KbCheck calls in the while-loop below? Or does the back tick
generated by the TTL pulse have an inherent duration that is longer than 1
millisecond?

Thanks for any feedback. Here are the two code bits I am comparing, and let's
assume that these MEX-files have been called and loaded into Matlab
memory previously:

--- KbCheck version ---

noTTLpulse = 1;
while (noTTLpulse)
[keyIsDown,secs,keyCode] = KbCheck;
if (keyIsDown)
if (keyCode(51))
noTTLpulse = 0;
end
end
end

--- GetChar version ---

noTTLpulse = 1;
while (noTTLpulse)
keyPressed = GetChar;
if (keyPressed == '`')
noTTLpulse = 0;
end
end
dear dan

try timing your loop. on my G4/500 powerbook the kbcheck loop
iterates at 14 kHz, which is plenty fast to detect a 1 ms event.

% program to time loop rate of kbcheck
noTTLpulse = 1;
i=0;
t=getsecs;
while i<10000
[keyIsDown,secs,keyCode] = KbCheck;
if keyIsDown
if keyCode(51)
noTTLpulse = 0;
break;
end
end
i=i+1;
end
t=getsecs-t;
fprintf('kbcheck loops at %.0f kHz.\n',i/t/1000);


I don't know how text is sent via USB, but it seems likely that the
timing is quantized. similarly, i haven't kept up with the ins and
outs of the several levels of time quantization that occur within the
Mac OS reception of characters, so it seems likely that the 1 ms
pulse will be extended by the time kbcheck sees it, but you've
already got plenty of margin, so missing the pulse is not an issue.

latency is harder to measure, because you lack ground truth. i would
suggest you find some way of getting your Matlab program to generate
a pulse to drive your puck, so that you can directly measure the
latency of both methods.

for the reasons you already know, i would expect kbcheck to be best,
however, i have no USB experience, so how things work out in practice
is still somewhat open.

this is an issue (timing of key input) that many people are
interested in, so please share with us all what you learn.

best

denis



>When a 1-millisecond-duration TTL pulse is sent to the BNC connector of our
>Rowland USB fMRI hand pucks, a back tick (`) keyboard character is generated.
>And when that happens, we would like our Matlab program to react as quickly as
>possible.
>
>I am trying to figure out which of the following is the better way to have my
>Matlab code pause, wait for that TTL pulse, and be the fastest off the bat. I
>have read what is out there on the differences between KbCheck and
>GetChar, but
>in this specific application, might GetChar be the better choice? That is, is
>it possible that the 1-millisecond-duration TTL pulse might be missed between
>successive KbCheck calls in the while-loop below? Or does the back tick
>generated by the TTL pulse have an inherent duration that is longer than 1
>millisecond?
>
>Thanks for any feedback. Here are the two code bits I am comparing, and let's
>assume that these MEX-files have been called and loaded into Matlab
>memory previously:
>
>--- KbCheck version ---
>
>noTTLpulse = 1;
>while (noTTLpulse)
> [keyIsDown,secs,keyCode] = KbCheck;
> if (keyIsDown)
> if (keyCode(51))
> noTTLpulse = 0;
> end
> end
>end
>
>--- GetChar version ---
>
>noTTLpulse = 1;
>while (noTTLpulse)
> keyPressed = GetChar;
> if (keyPressed == '`')
> noTTLpulse = 0;
> end
>end
>
>