-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvadeKeystrokes.xaml.cs
More file actions
275 lines (248 loc) · 9.43 KB
/
EvadeKeystrokes.xaml.cs
File metadata and controls
275 lines (248 loc) · 9.43 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2022 Plextora
// This file is licensed under GPL-v3.0
using Gma.System.MouseKeyHook;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using Button = System.Windows.Controls.Button;
using KeyEventArgs = System.Windows.Forms.KeyEventArgs;
#region Todo list
/*
* Clean up OnKeyDown and OnKeyUp functions
* (Maybe) fix redundant variables
*/
#endregion
namespace EvadeKeystrokes
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IKeyboardMouseEvents m_GlobalHook;
private static Brush BackgroundBorderValue;
private static Brush PressedValue;
private static Brush ForegroundValue;
private static string buttonBorderValue;
private static string pressedButtonValue;
private static string buttonForeground;
private readonly string DefaultPressedValue = "#FFBEE6FD";
private readonly string DefaultBackgroundBorderValue = "#FFDDDDDD";
private readonly string DefaultForegroundValue = "#FF000000";
private readonly Brush EmptyBrush = new BrushConverter().ConvertFrom("#00FFFFFF") as Brush;
private bool useBorder;
public MainWindow()
{
InitializeComponent();
Subscribe();
CreateConfig();
}
private void CreateConfig()
{
if (!File.Exists("config.txt"))
{
// actually creates the config.txt
File.Create("config.txt").Close();
string text =
"# 1st line is for button background\n" +
"# 2nd line is for button background/border when pressed\n" +
"# 3rd line is for button foreground\n" +
"# 4rd line is to tell Evade Keystrokes if you want to highlight the background or border when a key is pressed\n" +
"# For example, useborder:false would tell Evade Keystrokes to highlight the background\n" +
"# useborder:true would tell Evade Keystrokes to highlight the border\n\n" +
$"{DefaultBackgroundBorderValue}\n" +
$"{DefaultPressedValue}\n" +
$"{DefaultForegroundValue}\n" +
"useborder:false";
File.WriteAllText("config.txt", text);
// Loads default config
BackgroundBorderValue = new BrushConverter().ConvertFromString(DefaultBackgroundBorderValue) as Brush;
PressedValue = new BrushConverter().ConvertFromString(DefaultPressedValue) as Brush;
ForegroundValue = new BrushConverter().ConvertFromString(DefaultForegroundValue) as Brush;
}
else
{
LoadConfig();
}
}
private void LoadConfig()
{
// assign variables to lines from config.txt
buttonBorderValue = GetLine(8);
pressedButtonValue = GetLine(9);
buttonForeground = GetLine(10);
useBorder = GetLine(11) == "useborder:true";
/*
* I only use buttonBorderValue, pressedButtonValue, and buttonForeground once or twice.
* This also happens in another functions as well
* Kinda redundant?
* idk, probably wont fix this but it goes in the todo list anyways
*/
BackgroundBorderValue = new BrushConverter().ConvertFromString(buttonBorderValue) as Brush;
PressedValue = new BrushConverter().ConvertFromString(pressedButtonValue) as Brush;
ForegroundValue = new BrushConverter().ConvertFromString(buttonForeground) as Brush;
// Assigns all of the buttons backgrounds and foregrounds to BackgroundBorderValue and ForegroundValue
foreach (Button button in FindVisualChildren<Button>(WindowGrid))
{
button.Background = BackgroundBorderValue;
button.Foreground = ForegroundValue;
}
}
string GetLine(int line)
{
using (var sr = new StreamReader("config.txt"))
{
for (int i = 1; i < line; i++)
sr.ReadLine();
return sr.ReadLine();
}
}
/*
* I have no idea why or how it works but it works but thank you StackOverflow gods
* https://stackoverflow.com/a/978352/20083929
*/
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null) yield return (T)Enumerable.Empty<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
if (ithChild == null) continue;
if (ithChild is T t) yield return t;
foreach (T childOfChild in FindVisualChildren<T>(ithChild)) yield return childOfChild;
}
}
private void WindowClose(object sender, System.ComponentModel.CancelEventArgs e) => Unsubscribe();
// Make the button look like it's pressed using the user's config values
private void ActivateButton(Button buttonName)
{
if (useBorder)
{
buttonName.BorderBrush = PressedValue;
}
else
{
buttonName.Background = PressedValue;
}
}
// Make the button look normal using the user's config values
private void DeactivateButton(Button buttonName)
{
if (useBorder)
{
buttonName.BorderBrush = EmptyBrush;
}
else
{
buttonName.Background = BackgroundBorderValue;
}
}
public void Subscribe()
{
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.KeyDown += OnKeyDown;
m_GlobalHook.KeyUp += OnKeyUp;
}
// SPAGHETTI CODE INCOMING!!!
private void OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.W:
ActivateButton(WKeystroke);
break;
case Keys.A:
ActivateButton(AKeystroke);
break;
case Keys.S:
ActivateButton(SKeystroke);
break;
case Keys.D:
ActivateButton(DKeystroke);
break;
case Keys.Space:
ActivateButton(SpacebarKeystroke);
break;
case Keys.D1:
ActivateButton(OneKeystroke);
break;
case Keys.D2:
ActivateButton(TwoKeystroke);
break;
case Keys.R:
ActivateButton(RKeystroke);
break;
case Keys.T:
ActivateButton(TKeystroke);
break;
case Keys.LControlKey:
ActivateButton(CtrlorCKeystroke);
break;
case Keys.C:
ActivateButton(CtrlorCKeystroke);
break;
case Keys.E:
ActivateButton(EKeystroke);
break;
case Keys.Q:
ActivateButton(QKeystroke);
break;
}
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.W:
DeactivateButton(WKeystroke);
break;
case Keys.A:
DeactivateButton(AKeystroke);
break;
case Keys.S:
DeactivateButton(SKeystroke);
break;
case Keys.D:
DeactivateButton(DKeystroke);
break;
case Keys.Space:
DeactivateButton(SpacebarKeystroke);
break;
case Keys.D1:
DeactivateButton(OneKeystroke);
break;
case Keys.D2:
DeactivateButton(TwoKeystroke);
break;
case Keys.R:
DeactivateButton(RKeystroke);
break;
case Keys.T:
DeactivateButton(TKeystroke);
break;
case Keys.LControlKey:
DeactivateButton(CtrlorCKeystroke);
break;
case Keys.C:
DeactivateButton(CtrlorCKeystroke);
break;
case Keys.E:
DeactivateButton(EKeystroke);
break;
case Keys.Q:
DeactivateButton(QKeystroke);
break;
}
}
public void Unsubscribe()
{
m_GlobalHook.KeyDown -= OnKeyDown;
m_GlobalHook.Dispose();
}
private void WindowMove(object sender, MouseButtonEventArgs e) => DragMove();
}
}