-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathKeyProvider.cs
More file actions
209 lines (183 loc) · 7.77 KB
/
KeyProvider.cs
File metadata and controls
209 lines (183 loc) · 7.77 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Carnac.Logic.KeyMonitor;
using Carnac.Logic.Models;
using Microsoft.Win32;
using System.Windows.Media;
using SettingsProviderNet;
using System.Text.RegularExpressions;
using Carnac.Logic.MouseMonitor;
namespace Carnac.Logic
{
public class KeyProvider : IKeyProvider
{
readonly IInterceptKeys interceptKeysSource;
readonly IPasswordModeService passwordModeService;
readonly IDesktopLockEventService desktopLockEventService;
readonly PopupSettings settings;
string currentFilter = null;
private static readonly IList<Keys> modifierKeys =
new List<Keys>
{
Keys.LControlKey,
Keys.RControlKey,
Keys.LShiftKey,
Keys.RShiftKey,
Keys.LMenu,
Keys.RMenu,
Keys.ShiftKey,
Keys.Shift,
Keys.Alt,
Keys.LWin,
Keys.RWin
};
private bool winKeyPressed;
public KeyProvider(IInterceptKeys interceptKeysSource, IPasswordModeService passwordModeService, IDesktopLockEventService desktopLockEventService, ISettingsProvider settingsProvider)
{
if (settingsProvider == null)
{
throw new ArgumentNullException(nameof(settingsProvider));
}
this.interceptKeysSource = interceptKeysSource;
this.passwordModeService = passwordModeService;
this.desktopLockEventService = desktopLockEventService;
settings = settingsProvider.GetSettings<PopupSettings>();
}
private bool ShouldFilterProcess(out Regex filterRegex)
{
filterRegex = null;
if (settings?.ProcessFilterExpression != currentFilter)
{
currentFilter = settings?.ProcessFilterExpression;
if (!string.IsNullOrEmpty(currentFilter))
{
try
{
filterRegex = new Regex(currentFilter, RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(1));
}
catch
{
filterRegex = null;
}
}
else
{
filterRegex = null;
}
}
return (filterRegex != null);
}
public IObservable<KeyPress> GetKeyStream()
{
// We are using an observable create to tie the lifetimes of the session switch stream and the keystream
return Observable.Create<KeyPress>(observer =>
{
// When desktop is locked we will not get the keyup, because we track the windows key
// specially we need to set it to not being pressed anymore
var sessionSwitchStreamSubscription = desktopLockEventService.GetSessionSwitchStream()
.Subscribe(ss =>
{
if (ss.Reason == SessionSwitchReason.SessionLock)
winKeyPressed = false;
}, observer.OnError);
var keyStreamSubsription = Observable.Merge(
new IObservable<InterceptKeyEventArgs>[2] {
interceptKeysSource.GetKeyStream(),
InterceptMouse.Current.GetKeyStream() })
.Select(DetectWindowsKey)
.Where(k => k.KeyDirection == KeyDirection.Down)
.Select(ToCarnacKeyPress)
.Where(keypress => keypress != null)
.Where(k => !passwordModeService.CheckPasswordMode(k.InterceptKeyEventArgs))
.Subscribe(observer);
return new CompositeDisposable(sessionSwitchStreamSubscription, keyStreamSubsription);
});
}
InterceptKeyEventArgs DetectWindowsKey(InterceptKeyEventArgs interceptKeyEventArgs)
{
if (interceptKeyEventArgs.Key == Keys.LWin || interceptKeyEventArgs.Key == Keys.RWin)
{
if (interceptKeyEventArgs.KeyDirection == KeyDirection.Up)
winKeyPressed = false;
else if (interceptKeyEventArgs.KeyDirection == KeyDirection.Down)
winKeyPressed = true;
}
return interceptKeyEventArgs;
}
public static bool IsModifierKeyPress(InterceptKeyEventArgs interceptKeyEventArgs)
{
return modifierKeys.Contains(interceptKeyEventArgs.Key);
}
KeyPress ToCarnacKeyPress(InterceptKeyEventArgs interceptKeyEventArgs)
{
var process = AssociatedProcessUtilities.GetAssociatedProcess();
if (process == null)
{
return null;
}
// see if this process is one being filtered for
Regex filterRegex;
if (ShouldFilterProcess(out filterRegex) && !filterRegex.IsMatch(process.ProcessName))
{
return null;
}
if (!settings.ShowMouseClickKeys && (interceptKeyEventArgs.Key == Keys.LButton || interceptKeyEventArgs.Key == Keys.MButton || interceptKeyEventArgs.Key == Keys.RButton || interceptKeyEventArgs.Key == Keys.XButton1 || interceptKeyEventArgs.Key == Keys.XButton2))
{
return null;
}
if (!settings.ShowMouseScrollKeys && (interceptKeyEventArgs.Key == Keys.VolumeUp || interceptKeyEventArgs.Key == Keys.VolumeDown))
{
return null;
}
var isLetter = interceptKeyEventArgs.IsLetter();
var inputs = ToInputs(isLetter, winKeyPressed, interceptKeyEventArgs).ToArray();
try
{
string processFileName = process.MainModule.FileName;
ImageSource image = IconUtilities.GetProcessIconAsImageSource(processFileName);
return new KeyPress(new ProcessInfo(process.ProcessName, image), interceptKeyEventArgs, winKeyPressed, inputs);
}
catch (Exception)
{
return new KeyPress(new ProcessInfo(process.ProcessName), interceptKeyEventArgs, winKeyPressed, inputs); ;
}
}
static IEnumerable<string> ToInputs(bool isLetter, bool isWinKeyPressed, InterceptKeyEventArgs interceptKeyEventArgs)
{
var controlPressed = interceptKeyEventArgs.ControlPressed;
var altPressed = interceptKeyEventArgs.AltPressed;
var shiftPressed = interceptKeyEventArgs.ShiftPressed;
if (IsModifierKeyPress(interceptKeyEventArgs))
{
controlPressed = false;
altPressed = false;
shiftPressed = false;
isWinKeyPressed = false;
}
var mouseAction = InterceptMouse.MouseKeys.Contains(interceptKeyEventArgs.Key);
if (controlPressed)
yield return "Ctrl";
if (altPressed)
yield return "Alt";
if (isWinKeyPressed)
yield return "Win";
if (controlPressed || altPressed || mouseAction)
{
//Treat as a shortcut, don't be too smart
if (shiftPressed)
yield return "Shift";
yield return interceptKeyEventArgs.Key.SanitiseLower();
}
else
{
yield return interceptKeyEventArgs.Key.Sanitise();
}
}
}
}