-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathKeyProvider.cs
More file actions
179 lines (155 loc) · 6.69 KB
/
KeyProvider.cs
File metadata and controls
179 lines (155 loc) · 6.69 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
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;
namespace Carnac.Logic
{
public class KeyProvider : IKeyProvider
{
readonly IInterceptKeys interceptKeysSource;
readonly IPasswordModeService passwordModeService;
readonly IDesktopLockEventService desktopLockEventService;
readonly PopupSettings settings;
private 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 Regex GetRegEx()
{
if(settings?.ProcessFilterExpression == null)
{
return null;
}
return new Regex(settings?.ProcessFilterExpression, RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(1));
}
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 = interceptKeysSource.GetKeyStream()
.Select(DetectWindowsKey)
.Where(k => !IsModifierKeyPress(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;
}
bool IsModifierKeyPress(InterceptKeyEventArgs interceptKeyEventArgs)
{
return modifierKeys.Contains(interceptKeyEventArgs.Key);
}
KeyPress ToCarnacKeyPress(InterceptKeyEventArgs interceptKeyEventArgs)
{
var process = AssociatedProcessUtilities.GetAssociatedProcess();
if (process == null)
{
return null;
}
Debug.WriteLine("processName: " + process.ProcessName);
var filterRegex = GetRegEx();
if (filterRegex != null && !filterRegex.IsMatch(process.ProcessName))
{
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 (controlPressed)
yield return "Ctrl";
if (altPressed)
yield return "Alt";
if (isWinKeyPressed)
yield return "Win";
if (controlPressed || altPressed)
{
//Treat as a shortcut, don't be too smart
if (shiftPressed)
yield return "Shift";
yield return interceptKeyEventArgs.Key.Sanitise();
}
else
{
string input;
var shiftModifiesInput = interceptKeyEventArgs.Key.SanitiseShift(out input);
if (!isLetter && !shiftModifiesInput && shiftPressed)
yield return "Shift";
if (interceptKeyEventArgs.ShiftPressed && shiftModifiesInput)
yield return input;
else if (isLetter && !interceptKeyEventArgs.ShiftPressed)
yield return interceptKeyEventArgs.Key.ToString().ToLower();
else
yield return interceptKeyEventArgs.Key.Sanitise();
}
}
}
}