MATLAB: How to record two simultaneous key responses? (using Psychtoolbox)

key responsesMATLAB

In my experiment design, a pair of participants should press different keys (one should press 'z', the other should press '/' on the same keyboard) to respond to the signal appearing on the same screen. But I want to know how to record two different key responses with very short time intevals.
Hope I can get some advice on how to fix the code below:
while true
[~,~,keyCode]=KbCheck;
if keyCode(KbName('z'))
endtime1=GetSecs
end
end
while true
[~,~,keyCode]=KbCheck;
if keyCode(KbName('/'))
endtime2=GetSecs
end
i=i+1;
break;
end

Best Answer

gotem = false(1,2)
times = zeroes(1,2)
while ~all(gotem)
get a key
[tf,idx] = ismember(the key, appropriate values )
if tf
gotem(idx) = true
times(idx) = GetSecs
end
end
Note that in this code if the same key is pressed multiple times then the last is recorded . Recording the first instead would be easy
Related Question