forked from kurumpa/dotSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseHook.cs
63 lines (56 loc) · 1.54 KB
/
MouseHook.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Text;
namespace dotSwitcher
{
public class MouseHook
{
public event EventHandler<EventArgs> MouseEvent;
private IntPtr hookId = IntPtr.Zero;
private HookProc callback;
public MouseHook()
{
callback = ProcessMouse;
}
public bool IsStarted()
{
return hookId != IntPtr.Zero;
}
public void Start()
{
if (IsStarted()) { return; }
hookId = LowLevelAdapter.SetHook(LowLevelAdapter.WH_MOUSE_LL, callback);
}
public void Stop()
{
if (!IsStarted()) { return; }
LowLevelAdapter.ReleaseHook(hookId);
hookId = IntPtr.Zero;
}
private void OnMouseEvent(EventArgs e)
{
if (MouseEvent != null)
{
MouseEvent(this, e);
}
}
private IntPtr ProcessMouse(int nCode, IntPtr wParam, IntPtr lParam)
{
try
{
if (nCode >= 0)
{
switch (wParam.ToInt32())
{
case LowLevelAdapter.WM_LBUTTONDOWN:
case LowLevelAdapter.WM_RBUTTONDOWN:
OnMouseEvent(new EventArgs());
break;
}
}
}
catch { }
return LowLevelAdapter.NextHook(nCode, wParam, lParam);
}
}
}