title: Using KbQueueCheck to continuously check for device input
body: <p>I am using PsychToolbox to run an experiment on a fMRI scanner. I want to collect the timepoints of pulses sent out by the scanner. If you are not familiar with this topic: A scanner gives out a signal - equivalent to pressing keyboard digit "5" - in defined time-intervals. The first such signal starts the code, such that measurements by the scanner, and starting time of the code are synchronized. My code is sequential, as sketched out below. How could I implement a loop which at any time checks for these inputed "5", even though my main code runs in a "for"-loop?</p>
  
  <p>My code:</p>
  
  <pre><code>% here I wait for the scanner to input a "5", then my code will start
  KbQueueCreate;
  KbQueueStart;
  Trigger = KbName('5%');
  
  keyboard = -1;
  AllowedKeys = zeros(256,1); %Disable all keys
  AllowedKeys([LeftPress RightPress MiddlePress]) = 1; %Also allow escape always.
  
  
  while 1
  
        [pressed, firstPress] = KbQueueCheck();
  
      if firstPress(Trigger)
          startExperiment = GetSecs;
  break
      end
  end
  
  
  KbQueueStop;
  KbQueueRelease;
  
  % In the foolowing loop, the main experiment runs. here I show a screen, 
  % signal etc.
  
  for i = 1:nrTrials
  
  
  % here I would like to have code checking for scanner inputs
  
  end
  </code></pre>
  
  <p>The function KbQueueCheck should check for any keyboard input starting from the most recent call to KbQueueStart.
  Any input is very welcome.</p>