GlobalMouseHook hook = new GlobalMouseHook(); hook.StatusChanged += OnStatusChanged; hook.Start();
privatevoidOnStatusChanged(sender s, MouseHookEventArgs e) { // X Position = e.X // Y Possition = e.Y
if (e.MessageType == MouseHookMessageType.MouseMove) { // Do Something
DispatcherQueue.TryEnqueue(() => { // Update UI Here }); }
if (e.MessageType == MouseHookMessageType.LeftButtonDown) { // Do Something } }
Since the GlobalMouseHook operates on a different thread, you’ll need to update the UI in the StatusChanged event via a DispatcherQueue. Failing to do so will result in an exception.
Example with HookLevel
You can restrict the hook to your application window by specifying the TargetWindowHandle and Hook Level.