I want to write an application that counts number of mouse clicks in windows. After some research I have found this library: MouseKeyHook I have created wpf application and copied the example code:
public partial class MainWindow : Window
{
private IKeyboardMouseEvents m_GlobalHook;
public MainWindow()
{
InitializeComponent();
Subscribe();
}
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;
}
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();
}
}
There are no errors or warnings, and the application detects clicks and keys typed. But after few clicks I get the error message:
Sometimes this error shows after after just a few clicks, sometimes after 100 clicks (MouseDown event - left mouse button). Also when the error occures my cursor slow down significantly for couple of seconds.