Ignoring keyboard press if several keys are pressed simultaneously?
4 views (last 30 days)
Show older comments
Hi, I'm trying to collect keyboard inputs in an experiment (using PsychToolbox) and ran into the issue of simultaneous keypresses. I'm using KbCheck to verify that one of the two input keys has been pressed, but at the moment this also allows through responses where the participant has pressed both keys simultaneously. This makes my experiment crash later on, as a later image stimulus for the trial will be chosen based on the response made at the start of the trial.
Relevant parts of my code:
x = KbName('f');
y = KbName('j');
keyCode = zeros(1,256);
while ~keyCode(x) && ~keyCode(y)
[~, respTime, keyCode] = KbCheck;
end;
keyPressed = find(keyCode);
if keyPressed == x;
image = 1;
elseif keyPressed == y;
image = 2;
end
Is there a way to circumenvent this other than including a piece of code that recognized when both response buttons have been recorded and that then isntructs the participant to not mash the keyboard, after which the stimulus for the trial will be chosen at random?
0 Comments
Answers (1)
Guillaume
on 3 Apr 2018
Is there a way to circumenvent this other than including a piece of code
No, but that piece of code is trivial to write. In any case, your current code is really not reliable (if both e and f are pressed, it'll exit the while loop but your if test won't detect any key press) let's rewrite it all:
%side note: I don't understand the thought process that leads to naming the key code for 'f' x
%nor naming the key code for 'j' y.
%let's use some more logical names
codef = KbName('f');
codej = KbName('j');
keyCode = zeros(1, 256);
while true %loop for checking the keyboard is not mashed
while ~keyCode(codef) && ~keyCode(codej) = kbCheck;
[~, respTime, keyCode] = KbCheck;
end
if sum(keyCode) == 1 %only one key pressed
break; %exit loop
else
disp('Please only press one key');
end
end
%at this point we're guaranteed that only one of f or j has been pressed
if keyCode(codef)
image = 1;
else
image = 2;
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!