Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:09:46+00:00 2026-06-17T13:09:46+00:00

What I’m trying to achieve is to handle some complex key press and release

  • 0

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.

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 http://globalmousekeyhook.codeplex.com/ 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).

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:

var keyDownSeq = Observable.FromEventPattern<KeyEventArgs>(m_KeyboardHookManager, "KeyDown");
var keyUpSeq = Observable.FromEventPattern<KeyEventArgs>(m_KeyboardHookManager, "KeyUp");

var ctrlDown = keyDownSeq.Where(ev => ev.EventArgs.KeyCode == Keys.LControlKey).Select(_ => true);
var ctrlUp = keyUpSeq.Where(ev => ev.EventArgs.KeyCode == Keys.LControlKey).Select(_ => false);

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

Observable.Merge(ctrlDown, ctrlUp)                
    .Do(b => globabl_bool = b)
    .Subscribe();

But I think it ruins the whole Rx approach. Any ideas on how to achieve that while staying in Rx paradigm?

Then while the Ctrl is down I need to capture two Insert presses within a given time. I was thinking about using the Buffer:

var insertUp = keyUpSeq.Where(ev => ev.EventArgs.KeyCode == Keys.Insert);
insertUp.Buffer(TimeSpan.FromSeconds(1), 2)
    .Do((buffer) => { if (buffer.Count == 2) Debug.WriteLine("happened"); })
    .Subscribe();

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.

So once again, I need to keep track of double Insert press while Ctrl is down. Am I going in the right direction?

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?

EDIT: 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:09:47+00:00Added an answer on June 17, 2026 at 1:09 pm

    Firstly, welcome to the brain-bending magic of the Reactive Framework! 🙂

    Try this out, it should get you started on what you’re after – comments in line to describe whats going on:

    using(var hook = new KeyboardHookListener(new GlobalHooker()))
    {
        hook.Enabled = true;
        var keyDownSeq = Observable.FromEventPattern<KeyEventArgs>(hook, "KeyDown");
        var keyUpSeq = Observable.FromEventPattern<KeyEventArgs>(hook, "KeyUp");    
    
        var ctrlPlus =
            // Start with a key press...
            from keyDown in keyDownSeq
    
            // and that key is the lctrl key...
            where keyDown.EventArgs.KeyCode == Keys.LControlKey
    
            from otherKeyDown in keyDownSeq
                // sample until we get a keyup of lctrl...
                .TakeUntil(keyUpSeq
                    .Where(e => e.EventArgs.KeyCode == Keys.LControlKey))
    
                // but ignore the fact we're pressing lctrl down
                .Where(e => e.EventArgs.KeyCode != Keys.LControlKey)
            select otherKeyDown;
    
        using(var sub = ctrlPlus
               .Subscribe(e => Console.WriteLine("CTRL+" + e.EventArgs.KeyCode)))
        {
            Console.ReadLine();
        }
    }
    

    Now this doesn’t do exactly what you specified, but with a little tweaking, it could be easily adapted. The key bit is the implicit SelectMany calls in the sequential from clauses of the combined linq query – as a result, a query like:

    var alphamabits = 
        from keyA in keyDown.Where(e => e.EventArgs.KeyCode == Keys.A)
        from keyB in keyDown.Where(e => e.EventArgs.KeyCode == Keys.B)
        from keyC in keyDown.Where(e => e.EventArgs.KeyCode == Keys.C)
        from keyD in keyDown.Where(e => e.EventArgs.KeyCode == Keys.D)
        from keyE in keyDown.Where(e => e.EventArgs.KeyCode == Keys.E)
        from keyF in keyDown.Where(e => e.EventArgs.KeyCode == Keys.F)
        select new {keyA,keyB,keyC,keyD,keyE,keyF};
    

    translates (very) roughly into:

    if A, then B, then C, then..., then F -> return one {a,b,c,d,e,f}
    

    Make sense?

    (ok, since you’ve read this far…)

    var ctrlinsins =
        from keyDown in keyDownSeq
        where keyDown.EventArgs.KeyCode == Keys.LControlKey
        from firstIns in keyDownSeq
          // optional; abort sequence if you leggo of left ctrl
          .TakeUntil(keyUpSeq.Where(e => e.EventArgs.KeyCode == Keys.LControlKey))
          .Where(e => e.EventArgs.KeyCode == Keys.Insert)
        from secondIns in keyDownSeq
          // optional; abort sequence if you leggo of left ctrl
          .TakeUntil(keyUpSeq.Where(e => e.EventArgs.KeyCode == Keys.LControlKey))
          .Where(e => e.EventArgs.KeyCode == Keys.Insert)
        select "Dude, it happened!";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.