-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainView.cs
87 lines (76 loc) · 3.45 KB
/
MainView.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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LowLevelHooking;
namespace WinForms
{
// Normally I'd use MVVM with Windows Forms, but to keep the sample focused:
public partial class MainView : Form
{
private readonly HotstringHandler hotstringHandler = new HotstringHandler();
private readonly List<VirtualKeyEvent> hotstringSearch = new List<VirtualKeyEvent>();
private readonly List<VirtualKeyEvent> hotstringReplace = new List<VirtualKeyEvent>();
public MainView()
{
InitializeComponent();
Program.GlobalKeyboardHook.KeyDownOrUp += GlobalKeyboardHook_KeyDownOrUp;
Disposed += MainView_Disposed;
}
private void MainView_Disposed(object sender, EventArgs e)
{
// This isn't strictly necessary because MainView does not have a shorter lifetime
// than the whole application, but just in case something should change...
// Unsubscribing allows the garbage collector to free everything associated with MainView
// and of course, stops doing unnecessary work on each keypress system-wide.
Program.GlobalKeyboardHook.KeyDownOrUp -= GlobalKeyboardHook_KeyDownOrUp;
}
private void GlobalKeyboardHook_KeyDownOrUp(object sender, GlobalKeyboardHookEventArgs e)
{
logTextBox.AppendText($"{Environment.NewLine}{e.KeyCode} {(e.IsUp ? "up" : "down")}");
if (hotstringSearchTextBox.Focused || hotstringReplacementTextBox.Focused)
{
hotstringDisableButton.Checked = true;
if (hotstringSearchTextBox.Focused)
{
HandleHotkeyInput(hotstringSearchTextBox, e.KeyCode, e.IsUp, hotstringSearch);
hotstringHandler.Search = hotstringSearch;
}
else if (hotstringReplacementTextBox.Focused)
{
HandleHotkeyInput(hotstringReplacementTextBox, e.KeyCode, e.IsUp, hotstringReplace);
hotstringHandler.Replace = hotstringReplace;
}
}
else
{
hotstringHandler.ProcessKeyEvent(e);
}
}
private void hotstringEnableButton_CheckedChanged(object sender, EventArgs e)
{
hotstringHandler.Enabled = hotstringEnableButton.Checked;
}
private static void HandleHotkeyInput(TextBox textBox, VirtualKey keyCode, bool isUp, List<VirtualKeyEvent> keyList)
{
if (textBox.SelectionStart == 0 && textBox.SelectionLength == textBox.TextLength)
{
keyList.Clear();
textBox.Clear();
}
keyList.Add(new VirtualKeyEvent(keyCode, isUp));
// Write some pseudo-markup showing the nesting (for example, <LeftShift><A/></LeftShift>)
if (keyList.Count != 1 && keyList[keyList.Count - 2].Code == keyCode && !keyList[keyList.Count - 2].IsUp)
{
var prevText = textBox.Text;
textBox.Text = isUp
? prevText.Substring(0, prevText.Length - 1) + "/>"
: prevText.Substring(0, prevText.Length - 1) + $"/><{keyCode}>";
textBox.Select(textBox.TextLength, 0);
}
else
{
textBox.AppendText($"{(isUp ? "</" : "<")}{keyCode}>");
}
}
}
}