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.04k stars 257 forks source link

Hooks do not trigger events and windows starts lagging. #147

Closed habetuz closed 3 years ago

habetuz commented 3 years ago

Hello folks, I currently work with the Steelseries GameSense SDK to make my own effects etc. for my keyboard and mouse. To light up my mouse and keyboard on clicks and presses, I use this library.

Unfortunately, the mouse and keyboard events don't get triggered. Also, my mouse starts lagging, and keyboard input gets delayed. The lag and the delay only stay for about half a minute. I suspect that windows removes the hooks because it detects the lag.

I also tried this example program and everything works fine there.

Here is the code:

private static readonly IKeyboardMouseEvents GlobalHook = Hook.GlobalEvents();
static InputManager()
{
    Logger.Log("Starting...", Logger.Type.Info);
    GlobalHook.KeyDown += KeyEvent;
    GlobalHook.MouseDownExt += MouseEvent;
    Logger.Log("Ready!", Logger.Type.Info);
}

And the event functions:

private static void KeyEvent(object sender, KeyEventArgs eventArgs)
{
    Logger.Log(eventArgs.KeyCode.ToString());
}
private static void MouseEvent(object sender, MouseEventArgs eventArgs)
{
    Logger.Log(eventArgs.Button.ToString());
}

You can find the whole class (and the project) here.

The constructor is the only thing that gets executed in the program.

What I found regarding the lag is that the event function has to be fast. That cannot be the problem in my case because the Logger.Log() function is fast, and the lag also occurs when using Console.WriteLine().

As I said, the example program runs fine. I tried copying the example code, but that made no difference. The only real difference between my program and the example program is that the example uses .Net Core but I use .Net Framework (4.8). Could that be the reason? If it is the reason, is there any way to use the library with .Net Framework? I look forward to any help.

habetuz commented 3 years ago

Answered thanks to Charlieface. I did not call Application.Run(new ApplicationContext()). A working example looks the following (on [usage example](private static void Main(string[] args))):

private IKeyboardMouseEvents m_GlobalHook;

public void Subscribe()
{
    // Note: for the application hook, use the Hook.AppEvents() instead
    m_GlobalHook = Hook.GlobalEvents();

    m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
    m_GlobalHook.KeyPress += GlobalHookKeyPress;
    Application.Run(new ApplicationContext());
    Console.ReadLine();
    Unsubscribe()
}

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

private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
{
    Console.WriteLine("MouseDown: \t{0}; \t System Timestamp: \t{1}", e.Button, e.Timestamp);

    // uncommenting the following line will suppress the middle mouse button click
    // if (e.Buttons == MouseButtons.Middle) { e.Handled = true; }
}

public void Unsubscribe()
{
    m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
    m_GlobalHook.KeyPress -= GlobalHookKeyPress;

    //It is recommened to dispose it
    m_GlobalHook.Dispose();
}

Question on Stackoverflow: https://stackoverflow.com/questions/67827007/windows-hooks-do-not-trigger-events-and-windows-starts-lagging-using-globalmou/67828935?noredirect=1#comment119891333_67828935