Delay after processing keyboard event

Oct 13, 2011 at 7:15 AM

First, thank you so much for working-on and refining this very useful component.

I found it via a reference on StackOverflow.  It seems that there are a number of articles on SO that could benefit from a link to this component.

I've run into an issue, and I've seen a few references to the same (or similar?) on your codeproject page, but I can't seem to figure it out.

I'm hooking the keyboard.  I'm looking for a specific key (F10).  When F10 is pressed, I do a bunch of stuff [DoStuff();], including opening a modal form.  Everything works great, with one issue.

The issue is that there is a delay (a second or two or three) when I call DoStuff();. I believe the delay has something to do with your component.

I've tried moving the keyboard hook to a new thread (using various methods (threading and backgroundworker), but when doing so, the KeyDown event doesn't get fired.

I also tried (temporarily to see if it changed the situation) opening the form regularly (not modal), which didn't help.

Any help would be greatly appreciated. 

Oct 14, 2011 at 12:54 AM

Can you tell me what operating system and architecture are you using? 

The delay you're talking about - does it cause the mouse to "freeze-up" for those second(s)?

Oct 14, 2011 at 3:06 AM

Windows 7, 32 bit.  .NET Framework 3.0, C#

The mouse does not freeze up, just the execution of my app.  When I call DoStuff() (from the KeyDown event), I load a form.  The Form_Load runs right away, but it takes about 3 seconds for the form to appear.

If I disable the keyboard hook and just load the form as usual, it appears immediately.

Oct 22, 2011 at 6:23 PM

sPhinX on Codeproject posted a suggestion for you.

"I think the root cause of delay is the form creation, he said "opening a modal form", I think that he use "new Form", and "ShowModal", I use same code someday ago, so I get a delay too. I try to fix it, at last I solve it. I create a form first and hide it, then show it when the hook key pressed. That's be ok. I wish this can help him." - sPhinX

Dec 17, 2011 at 5:05 AM


I was able to reliably reproduce this problem!

Here's how to do it:

Open the MouseKeyboardActivityMonitor.Demo project in Visual Studio.

Add a new form to the project (Ex: FrmTest).

Open TestFormHookListeners.cs (code) and change HookManager_KeyDown to look like this:


        private void HookManager_KeyDown(object sender, KeyEventArgs e)
            Log(string.Format("KeyDown \t\t {0}\n", e.KeyCode));

            if (e.KeyCode == Keys.F10)
                FrmTest fTest = new FrmTest();




Now, when you run the app:

  • Click the Keydown checkbox
  • Click in the log area
  • Type a bunch of stuff
  • Press F10
  • The modal dialog will load, and it will freeze the screen for about 3 seconds.

See these bug videos - I put a animated gif spinner so you could see freezing:

When trying to load a modal form (.ShowDialog):

When trying to load a non-modal form (.Show):


So, clearly trying to load a form modally is a problem.  But it's not the only way to cause this freezing.  I removed the modal forms from my more complicated project and it didn't fix the freezing...

Thanks for your hard work on this.  A fix would be really great.