title: Reactive extensions: matching complex key press sequence
body: <p>What I'm trying to achieve is to handle some complex key press and release sequence with Rx. I have some little experience with Rx, but it's clearly not enough for my current undertaking, so I'm here for some help.</p>
  
  <p>My WinForms app is running in the background, only visible in a system tray. By a given key sequence I want to activate one of it's forms. Btw, to hook up to the global key presses I'm using a nice library <a href="http://globalmousekeyhook.codeplex.com/" rel="nofollow">http://globalmousekeyhook.codeplex.com/</a> I'm able to receive every key down and key up events, and while key is down multiple KeyDown events are produced (with a standard keyboard repeat rate).</p>
  
  <p>One of example key sequence I want to capture is a quick double Ctrl + Insert key presses (like holding Ctrl key and pressing Insert twice in a given period of time). Here is what I have currently in my code:</p>
  
  <pre><code>var keyDownSeq = Observable.FromEventPattern&lt;KeyEventArgs&gt;(m_KeyboardHookManager, "KeyDown");
  var keyUpSeq = Observable.FromEventPattern&lt;KeyEventArgs&gt;(m_KeyboardHookManager, "KeyUp");
  
  var ctrlDown = keyDownSeq.Where(ev =&gt; ev.EventArgs.KeyCode == Keys.LControlKey).Select(_ =&gt; true);
  var ctrlUp = keyUpSeq.Where(ev =&gt; ev.EventArgs.KeyCode == Keys.LControlKey).Select(_ =&gt; false);
  </code></pre>
  
  <p>But then I'm stuck. My idea is that I need somehow to keep track of if the Ctrl key is down. One way is to create some global variable for that, and update it in some Merge listener</p>
  
  <pre><code>Observable.Merge(ctrlDown, ctrlUp)                
      .Do(b =&gt; globabl_bool = b)
      .Subscribe();
  </code></pre>
  
  <p>But I think it ruins the whole Rx approach. Any ideas on how to achieve that while staying in Rx paradigm?</p>
  
  <p>Then while the Ctrl is down I need to capture two Insert presses within a given time. I was thinking about using the Buffer:</p>
  
  <pre><code>var insertUp = keyUpSeq.Where(ev =&gt; ev.EventArgs.KeyCode == Keys.Insert);
  insertUp.Buffer(TimeSpan.FromSeconds(1), 2)
      .Do((buffer) =&gt; { if (buffer.Count == 2) Debug.WriteLine("happened"); })
      .Subscribe();
  </code></pre>
  
  <p>However I'm not sure if it's most efficient way, because Buffer will produce events every one second, even if there was no any key pressed. Is there a better way? And I also need to combine that with Ctrl down somehow.</p>
  
  <p>So once again, I need to keep track of double Insert press while Ctrl is down. Am I going in the right direction?</p>
  
  <p>P.S. another possible approach is to subscribe to Insert observable only while Ctrl is down. Not sure how to achieve that though. Maybe some ideas on this as well?</p>
  
  <p><strong>EDIT:</strong> Another problem I've found is that Buffer doesn't suit my needs exactly. The problem comes from the fact that Buffer produces samples every two seconds, and if my first press belongs to the first buffer, and second to the next one, then nothing happens. How to overcome that?</p>