-
Notifications
You must be signed in to change notification settings - Fork 16
/
Context.cs
496 lines (461 loc) · 18.5 KB
/
Context.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using Loamen.KeyMouseHook;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using System.Windows;
using System.IO;
using System.Threading.Tasks;
using Adb_gui_Apkbox_plugin;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Components;
using MessageBox = System.Windows.Forms.MessageBox;
namespace Kling
{
public class Context : ApplicationContext
{
private Window _hiddenWindow;
private System.ComponentModel.IContainer _components;
private NotifyIcon _notifyIcon;
private display keyui;
Timer timer;
private ContextMenuStrip _contextmenustrip;
private IKeyboardMouseEvents m_GlobalHook;
private readonly KeyMouseFactory eventHookFactory = new KeyMouseFactory(Hook.GlobalEvents());
private readonly KeyboardWatcher keyboardWatcher;
private List<MacroEvent> _macroEvents;
bool isaboutshowing = false, specialkeys = false, suppresskey = false, suppresskey2 = false, settingshowing = false,
notify = true, stdkeys = true; int displaytime = 2; bool record = true; bool logkeys = false;
System.Drawing.Point location; Components.SettingsUI settingsui;
private float opacity = 0.7f;
public Context()
{
_components = new System.ComponentModel.Container();
ExtractNecessaryResources();
// Load Settings, but first create if not exist
if (!File.Exists(@"config.ini"))
{
var height = SystemInformation.VirtualScreen.Height;
File.WriteAllText(@"config.ini",
"[Settings]" + Environment.NewLine +
"displayindex=2" + Environment.NewLine +
"xaxis=20" + Environment.NewLine +
"yaxis=" + (height - new display().Height - 60) + Environment.NewLine +
"displaytime=2" + Environment.NewLine +
"notify=True" + Environment.NewLine +
"logkeys=True" + Environment.NewLine +
"opacity=0.7" + Environment.NewLine +
"stdkeys=True" + Environment.NewLine);
}
if (File.Exists(@"config.ini"))
{
var myini = new IniFile(@"config.ini");
location = new System.Drawing.Point(
Convert.ToInt16(myini.Read("xaxis", "Settings")),
Convert.ToInt16(myini.Read("yaxis", "Settings"))
);
displaytime = Convert.ToInt16(myini.Read("displaytime", "Settings"));
notify = Convert.ToBoolean(myini.Read("notify", "Settings"));
stdkeys = Convert.ToBoolean(myini.Read("stdkeys", "Settings"));
logkeys = Convert.ToBoolean(myini.Read("logkeys", "Settings"));
opacity = Convert.ToSingle(myini.Read("opacity", "Settings"));
}
LoadKeyConfigs();
keyui = new display();
keyui.Opacity = opacity;
keyui.Location = location;
_contextmenustrip = new ContextMenuStrip();
_contextmenustrip.Items.Add(NewToolStripItem("Stop recording", (o, s) =>
{
var firstItem = _contextmenustrip.Items[0];
if (record)
{
// Stop Recording
record = false;
firstItem.Text = "Start recording";
DisplayStatusMessage("Kling : Service stopped");
}
else
{
// Start Recording
record = true;
firstItem.Text = "Stop recording";
DisplayStatusMessage("Kling : Service started");
}
}));
_contextmenustrip.Items.Add(new ToolStripSeparator());
_contextmenustrip.Items.Add(NewToolStripItem("Settings", ShowSettings));
_contextmenustrip.Items.Add(NewToolStripItem("Restart", (o, s) => { System.Windows.Forms.Application.Restart(); }));
_contextmenustrip.Items.Add(NewToolStripItem("About", (o, s) =>
{
// About screen dialog
if (!isaboutshowing)
{
isaboutshowing = true;
Components.AboutUI ui = new Components.AboutUI();
ui.Closing += (obj, ex) => { isaboutshowing = false; };
ui.ShowDialog();
}
}));
_contextmenustrip.Items.Add(new ToolStripSeparator());
_contextmenustrip.Items.Add(NewToolStripItem("Exit", (o, s) =>
{
// Exit Button
if (eventHookFactory != null)
eventHookFactory.Dispose();
System.Windows.Forms.Application.Exit();
}));
_notifyIcon = new NotifyIcon(_components)
{
ContextMenuStrip = _contextmenustrip,
Icon = Kling.Properties.Resources.icon,
Text = "Kling",
Visible = true,
};
_notifyIcon.DoubleClick += ShowSettings;
_hiddenWindow = new System.Windows.Window();
_hiddenWindow.Hide();
DisplayStatusMessage(_notifyIcon.Text + ": Start pressing keys");
keyboardWatcher = eventHookFactory.GetKeyboardWatcher();
keyboardWatcher.OnKeyboardInput += (s, e) =>
{
if (!record)
return;
if (_macroEvents != null)
_macroEvents.Add(e);
if (e.KeyMouseEventType == MacroEventType.KeyPress)
{
var keyEvent = (KeyPressEventArgs)e.EventArgs;
if (e.KeyMouseEventType.ToString().Contains("KeyUp"))
{
// This will also show a form
DisplayKeys(getKeys(keyEvent.KeyChar.ToString()));
}
}
else
{
var keyEvent = (System.Windows.Forms.KeyEventArgs)e.EventArgs;
if (e.KeyMouseEventType.ToString().Contains("KeyUp"))
{
// This will show a form
var keys = keyEvent.KeyCode;
// Suppress next event
if (suppresskey)
{
suppresskey = false;
return;
}
if (suppresskey2)
{
suppresskey2 = false;
return;
}
if (isControlPressed(keyEvent, keys))
{
specialkeys = true;
if (isShiftPressed(keyEvent, keys))
{
if (isAltPressed(keyEvent, keys))
{
suppresskey2 = true;
showKey("Ctrl + Shift + Alt + ", keys);
}
else showKey("Ctrl + Shift + ", keys);
}
else if (isAltPressed(keyEvent, keys))
{
if (isShiftPressed(keyEvent, keys))
{
suppresskey2 = true;
showKey("Ctrl + Alt + Shift + ", keys);
}
else showKey("Ctrl + Alt + ", keys);
}
else DisplayKeys("Ctrl + " + getKeys(keys.ToString()));
}
else if (isAltPressed(keyEvent, keys))
{
specialkeys = true;
if (isShiftPressed(keyEvent, keys))
{
if (isControlPressed(keyEvent, keys))
{
suppresskey2 = true;
showKey("Alt + Shift + Ctrl + ", keys);
}
else showKey("Alt + Shift + ", keys);
}
else if (isControlPressed(keyEvent, keys))
{
if (isShiftPressed(keyEvent, keys))
{
suppresskey2 = true;
showKey("Alt + Ctrl + Shift + ", keys);
}else
showKey("Alt + Ctrl + ", keys);
}
else DisplayKeys("Alt + " + getKeys(keys.ToString()));
}
else if (isShiftPressed(keyEvent, keys))
{
specialkeys = true;
if (isControlPressed(keyEvent, keys))
{
if (isAltPressed(keyEvent, keys))
{
suppresskey2 = true;
showKey("Shift + Ctrl + Alt + ", keys);
}else showKey("Shift + Ctrl + ", keys);
}
else if (isAltPressed(keyEvent, keys))
{
if (isControlPressed(keyEvent, keys))
{
suppresskey2 = true;
showKey("Shift + Alt + Ctrl + ", keys);
}else showKey("Shift + Alt + ", keys);
}
else DisplayKeys("Shift + " + getKeys(keys.ToString()));
}
else
{
if (!specialkeys)
{
DisplayKeys(getKeys(keys.ToString()));
}
else specialkeys = false;
}
}
}
};
_macroEvents = new List<MacroEvent>();
keyboardWatcher.Start(Hook.GlobalEvents());
timer = new Timer();
timer.Interval = displaytime * 1000;
timer.Tick += async (o, e) =>
{
timer.Stop();
while (keyui.Opacity > 0.0)
{
await Task.Delay(20);
keyui.Opacity -= 0.05;
}
keyui.Opacity = 0;
keyui.Hide();
keyui.Opacity = opacity;
};
//Subscribe();
//ThreadExit += (o, e) =>
//{
// Unsubscribe();
//};
}
private List<CodeUI> windows = new List<CodeUI>();
public void Subscribe()
{
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
m_GlobalHook.KeyPress += GlobalHookKeyPress;
m_GlobalHook.KeyDown += M_GlobalHook_KeyDown;
}
private void M_GlobalHook_KeyDown(object sender, KeyEventArgs e)
{
foreach (var ui in windows)
{
ui.PushUp();
}
var codeUI = new CodeUI(displaytime)
.SetText(e.KeyCode.ToString());
codeUI.Closing += (o, ex) => { windows.Remove(codeUI); };
//.SetLocation(location.X, location.Y);
codeUI.Show();
windows.Add(codeUI);
Debug.WriteLine("KeyDown: " + e.KeyCode.ToString());
}
private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
Debug.WriteLine("KeyPress: \t{0}", e.KeyChar);
}
private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
{
Debug.WriteLine("MouseDown: \t{0}; \t System Timestamp: \t{1}", e.Button, e.Timestamp);
// uncommenting the following line will suppress the middle mouse button click
// if (e.Buttons == MouseButtons.Middle) { e.Handled = true; }
}
public void Unsubscribe()
{
m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
m_GlobalHook.KeyPress -= GlobalHookKeyPress;
//It is recommened to dispose it
m_GlobalHook.Dispose();
}
private void showKey(string Text, Keys keys)
{
suppresskey = true;
DisplayKeys(Text + getKeys(keys.ToString()));
}
private bool isControlPressed(KeyEventArgs keyEvent, Keys keys)
{
return keyEvent.Control && keys != Keys.RControlKey && keys != Keys.LControlKey &&
keys != Keys.Control && keys != Keys.ControlKey;
}
private bool isAltPressed(KeyEventArgs keyEvent, Keys keys)
{
return keyEvent.Alt && keys != Keys.RMenu && keys != Keys.LMenu &&
keys != Keys.Alt;
}
private bool isShiftPressed(KeyEventArgs keyEvent, Keys keys)
{
return keyEvent.Shift && keys != Keys.Shift && keys != Keys.LShiftKey &&
keys != Keys.RShiftKey && keys != Keys.ShiftKey;
}
private void DisplayKeys(string Text)
{
if (Text == null) return;
keyui.Hide();
timer.Stop();
keyui.SetText(Text);
if (logkeys)
File.AppendAllText("app.log", $"[{DateTime.Now.ToString()}] {Text}{Environment.NewLine}");
timer.Start();
keyui.Show();
}
public string getKeys(string Text)
{
if (!stdkeys)
return Text;
if (!DoesContainKey(Text)) return null;
if (Text.Length == 2)
{
if (Text.StartsWith("D"))
return Text.Substring(1);
}
else if (Text.StartsWith("NumPad"))
{
return Text.Substring(6);
}
switch (Text)
{
case "LMenu":
return "Alt";
case "RMenu":
return "Alt";
case "RControlKey":
return "Ctrl";
case "LControlKey":
return "Ctrl";
case "LShiftKey":
return "Shift";
case "RShiftKey":
return "Shift";
case "LWin":
return "Win";
case "RWin":
return "Win";
case "Add":
return "Num +";
case "Subtract":
return "Num -";
case "Divide":
return "Num /";
case "Multiply":
return "Num *";
// Oem Keys
case "OemMinus":
return "-";
case "Oemplus":
return "=";
case "Oemtilde":
return "`";
case "Oem5":
return "\\";
case "Oem6":
return "]";
case "OemOpenBrackets":
return "[";
case "Oem1":
return ";";
case "Oem7":
return "'";
case "Oemcomma":
return ",";
case "OemPeriod":
return ".";
case "OemQuestion":
return "/";
}
return Text;
}
private void ShowSettings(object sender, EventArgs e)
{
// Show Settings
if (!settingshowing)
{
settingsui = new Components.SettingsUI(keyui.Height, keyui.Width);
settingsui.Closing += (o, ex) => { settingshowing = false; };
settingsui.ShowDialog();
}
}
private ToolStripMenuItem NewToolStripItem(string Text, EventHandler handler)
{
var item = new ToolStripMenuItem(Text);
if (handler != null)
{
item.Click += handler;
}
return item;
}
private void DisplayStatusMessage(string text, string message = null)
{
_hiddenWindow.Dispatcher.Invoke(delegate
{
if (notify)
{
_notifyIcon.BalloonTipText = text;
if (message != null)
_notifyIcon.Text = message;
// The timeout is ignored on recent Windows
_notifyIcon.ShowBalloonTip(3000);
}
});
}
private List<String> showKeys = new List<string>();
private void LoadKeyConfigs()
{
var lines = File.ReadAllLines(@"keys.txt")
.Where(c => !c.StartsWith("#") && !c.Contains(".") && !string.IsNullOrWhiteSpace(c)).ToList();
foreach (var line in lines)
{
try
{
var items = line.Split('\t');
if (items[2] == "1") showKeys.Add(items[0]);
}
catch
{
MessageBox.Show($"Couldn't parse line {line}, incorrect characters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private bool DoesContainKey(string Text)
{
if (showKeys.Contains(Text)) return true;
return false;
}
private void ExtractNecessaryResources()
{
// King.Resources.keys.txt
if (!File.Exists(@"keys.txt"))
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Kling.Resources.keys.txt"))
using (var reader = new StreamReader(stream))
{
File.WriteAllText(@"keys.txt", reader.ReadToEnd());
}
}
}
}
}