title: MATLAB: Pause program and await keypress
body: <p>I am writing a program in which at some point a graph is plotted and displayed on screen. The user then needs to press 'y' or 'n' to accept or reject the graph. My current solution uses the PsychToolbox (the actual solution doesn't need to), which includes a command called 'KbCheck' which checks at the time of calling the state of all the keyboard buttons. My code looks like this:</p>
  
  <pre><code>function [keyPressed] = waitForYesNoKeypress
  keyPressed = 0; % set this to zero until we receive a sensible keypress
  while keyPressed == 0 % hang the system until a response is given
      [ keyIsDown, seconds, keyCode ] = KbCheck; % check for keypress
      if find(keyCode) == 89 | find(keyCode) == 78 % 89 = 'y', 78 = 'n'
          keyPressed = find(keyCode);
      end
  end
  </code></pre>
  
  <p>The problem is, that the system really does 'hang' until a key is pressed. Ideally, I would be able to scroll, zoom, and generally interact with the graphs that are plotted onscreen so that I can really decide whether or not I want to press 'y' or 'n'!</p>
  
  <p>I have tried adding 'drawnow;' into the while loop above but that doesn't work: I still am unable to interact with the plotted graphs until <em>after</em> I've accepted or rejected them.</p>
  
  <p>The solution doesn't have to use PsychToolbox; I assume there are plenty of other options out there?</p>
  
  <p>Thanks</p>