Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know if it's too late, but I have an answer for you.</p> <p>The Rx extension method you need to use is <code>BufferWithCount</code>.</p> <p>I'll assume you know how to turn key press events into an <code>IObservable&lt;char&gt;</code>.</p> <p>So given you have a list of character sequences that you want to detect and then perform an action I suggest using a <code>Dictionary&lt;string, Action&gt;</code> to hold this data, like so:</p> <pre><code>var matches = new Dictionary&lt;string, Action&gt;() { { "ba", () =&gt; Console.WriteLine("ba") }, { "aba", () =&gt; Console.WriteLine("aba") }, { "baa", () =&gt; Console.WriteLine("baa") }, { "abc\t", () =&gt; Console.WriteLine("abc\\t") }, }; </code></pre> <p>So here's the Rx (and <code>IEnumerable</code>) queries required:</p> <pre><code>int max = matches .Select(m =&gt; m.Key.Length) .Max(); IObservable&lt;string&gt; chords = Enumerable .Range(2, max - 1) .Select(n =&gt; keys .BufferWithCount(n, 1) .Select(cs =&gt; new string(cs.ToArray()))) .Merge(); IObservable&lt;Action&gt; actions = chords .Where(s =&gt; matches.ContainsKey(s)) .Select(s =&gt; matches[s]); </code></pre> <p>So finally you just have an <code>IObservable&lt;Action&gt;</code> that you can subscribe to and you just invoke the <code>Action</code>.</p> <p>If you want to test out that this works use the following code:</p> <pre><code>IConnectableObservable&lt;char&gt; keys = "ababc\tdabaababc\tebad" .ToObservable() .Publish(); //`.Publish()` makes a cold observable become hot, // but you must call `Connect()` to start producing values. //insert above `matches` definition here. //insert above queries here. actions.Subscribe(a =&gt; a()); keys.Connect(); </code></pre> <p>The result should be:</p> <pre><code>ba aba abc\t ba aba baa ba aba abc\t ba </code></pre> <p>Enjoy!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload