Ghost1372

everything can be handy

GlobalMouseHook

A Low-Level Global Mouse Hook runs on a separate thread.

Events

Name
StatusChanged

Methods

Name
Start
Stop
Dispose

Property

Name
Level
TargetWindowHandle

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
GlobalMouseHook hook = new GlobalMouseHook();
hook.StatusChanged += OnStatusChanged;
hook.Start();

private void OnStatusChanged(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.

1
2
3
4
5
GlobalMouseHook hook = new GlobalMouseHook();
hook.TargetWindowHandle = myWindowHwnd;
hook.Level = HookLevel.Application;
hook.StatusChanged += OnStatusChanged;
hook.Start();

Demo

you can run demo and see this feature.

0%