function out = getKeyResp(respChar1, respChar2, anyKeyBool)

%waits for a keypress.  if anyKeyBool==T then any key works.  otherwise the key must match respChar1 or respChar 2.
%routine can also be halted through escapeSequence (see escapeSequence.m; requires pressing "QP").
%if routine is waiting just for a spacebar, subject is given a hint after 10 failed (non-spacebar) key presses.

beginTime = GetSecs;
responded = 0;
tries = 0;

while ~responded
   [keyIsDown,secs,keyCode] = KbCheck;
   
   while keyIsDown 
      [keyIsDown,secs,keyCode] = KbCheck; 
   end
   
   while ~keyIsDown
      [keyIsDown,secs,lastKey] = KbCheck;
   end
   
   key = KbName(lastKey);
   
   if escapeSequence(key)
      responded = 1;
      out = [-1 GetSecs-beginTime];
      disp 'Escape sequence activated'
   elseif isequal(upper(key), upper(respChar1))
      responded = 1;
      out = [0, GetSecs-beginTime];
   elseif isequal(upper(key), upper(respChar2))
      responded = 1;
      out = [1, getSecs-beginTime];
   elseif anyKeyBool
      responded = 1;
      out = [2, getSecs-beginTime];
   else tries=tries+1;  
   end
end

[keyIsDown,secs,keyCode] = KbCheck;
while keyIsDown
   [keyIsDown,secs,keyCode] = KbCheck;
end
