gmamaladze / globalmousekeyhook

This library allows you to tap keyboard and mouse, detect and record their activity even when an application is inactive and runs in background.
MIT License
1.06k stars 256 forks source link

Sleep inside KeyDown #74

Open tholder opened 7 years ago

tholder commented 7 years ago

It seems like you can't supress a keypress in the keydown event if there is any sort of delay?

Any idea what causes this?

KryukovDS commented 4 years ago

I hope I was not mistaken about the topic, and this same issue is mentioned here. I came across it when implementing key interception through SetWindowsHookEx:

[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevel_KeyboardProc Ipfn, IntPtr hMod, uint dwThreadId);

The problem is that if there is "Thread.Sleep" inside the event handler or somewhere inside its code, then after a couple of clicks the event completely stops registering. In this library, I encountered the same behavior.

private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
        Console.WriteLine("KeyPress: \t{0}", e.KeyChar);
        Thread.Sleep(500);
}