function  [resp,keySecs] = get_response(keys,flushp) ;

%GET_RESPONSE -- Get timed response from the keyboard.
%
%  [resp,keySecs] = get_response(keys,[flushp])
%
%  KEYS is a cell array of strings, interpreted as character sets.
%  RESP is the index of the first set that contains the key pressed by
%  the subject. If the subject presses an invalid key, RESP=0.
%  KEYSECS is the time stamp of the key press (as returned by KBCheck).
%
%  Warning: This routine does not work if MATLAB's "backgrounding" is
%           enabled. Type 'helpwin CharAvail' for details.
%
%  Example:
%    yesKeys = 'sdfSDF' ; noKeys = 'jklJKL' ;
%    startSecs = GetSecs;
%    PresentStumulus(...) ;
%    [resp,keySecs] = get_response({noKeys,yesKeys}) ;
%    RT = keySecs-startSecs ;
%    
%  See also KBCHECK, GETCHAR, GETSECS, ISMEMBER.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2001/12/13 21:24 $

if (nargin < 2)  flushp = 1 ; end
if (flushp)
   FlushEvents('keyDown') ;
end

while (1)
   [keyIsDown,keySecs] = KbCheck ;    % get the timing, fast
   if CharAvail
      c = GetChar ;                   % translate the char, slow
      break
   end
end

resp = 0 ;
for k = 1:length(keys)
   if (ismember(c,keys{k}))
      resp = k ;
      break
   end
end

%--- Return RESP and KEYSECS  
%%%%% End of file GET_RESPONSE.M
