-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathInterceptMouse.cs
More file actions
112 lines (99 loc) · 3.73 KB
/
InterceptMouse.cs
File metadata and controls
112 lines (99 loc) · 3.73 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Carnac.Logic.KeyMonitor;
using System;
using System.Diagnostics;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using System.Collections.Generic;
namespace Carnac.Logic.MouseMonitor
{
public class InterceptMouse : IInterceptKeys
{
public static readonly InterceptMouse Current = new InterceptMouse();
readonly IKeyboardMouseEvents m_GlobalHook = Hook.GlobalEvents();
readonly IObservable<InterceptKeyEventArgs> keyStream;
private IObserver<InterceptKeyEventArgs> observer;
private readonly KeysConverter kc = new KeysConverter();
public static readonly List<Keys> MouseKeys = new List<Keys>()
{
Keys.LButton,
Keys.MButton,
Keys.RButton,
Keys.XButton1,
Keys.XButton2,
Keys.VolumeUp,
Keys.VolumeDown
};
InterceptMouse()
{
keyStream = Observable.Create<InterceptKeyEventArgs>(observer =>
{
this.observer = observer;
m_GlobalHook.MouseClick += OnMouseClick;
m_GlobalHook.MouseDoubleClick += OnMouseDoubleClick;
m_GlobalHook.MouseWheel += HookManager_MouseWheel;
Debug.Write("Subscribed to mouse");
return Disposable.Create(() =>
{
m_GlobalHook.MouseClick -= OnMouseClick;
m_GlobalHook.MouseDoubleClick -= OnMouseDoubleClick;
m_GlobalHook.MouseWheel -= HookManager_MouseWheel;
m_GlobalHook.Dispose();
Debug.Write("Unsubscribed from mouse");
});
})
.Publish().RefCount();
}
private Keys MouseButtonsToKeys(MouseButtons button)
{
switch(button)
{
case MouseButtons.Left:
return Keys.LButton;
case MouseButtons.Middle:
return Keys.MButton;
case MouseButtons.Right:
return Keys.RButton;
case MouseButtons.XButton1:
return Keys.XButton1;
case MouseButtons.XButton2:
return Keys.XButton2;
default:
return Keys.None;
}
}
private void OnMouseClick(object sender, MouseEventArgs e)
{
observer.OnNext(new InterceptKeyEventArgs(
MouseButtonsToKeys(e.Button),
KeyDirection.Down,
Control.ModifierKeys == Keys.Alt,
Control.ModifierKeys == Keys.Control,
Control.ModifierKeys == Keys.Shift));
}
private void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
observer.OnNext(new InterceptKeyEventArgs(
MouseButtonsToKeys(e.Button),
KeyDirection.Down,
Control.ModifierKeys == Keys.Alt,
Control.ModifierKeys == Keys.Control,
Control.ModifierKeys == Keys.Shift));
}
private void HookManager_MouseWheel(object sender, MouseEventArgs e)
{
// for now using VolumeDown and Up as proxy could be refactored
observer.OnNext(new InterceptKeyEventArgs(
e.Delta > 0 ? Keys.VolumeUp : Keys.VolumeDown,
KeyDirection.Down,
Control.ModifierKeys == Keys.Alt,
Control.ModifierKeys == Keys.Control,
Control.ModifierKeys == Keys.Shift));
}
public IObservable<InterceptKeyEventArgs> GetKeyStream()
{
return keyStream;
}
}
}