-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
188 lines (150 loc) · 5.08 KB
/
Form1.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
using Quiccent.Constants;
using Quiccent.Handlers;
using Quiccent.Properties;
using Quiccent.Wrappers;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using WindowsInput;
using WindowsInput.Native;
namespace Quiccent
{
public partial class Form1 : Form
{
private int _characterSelectionIndex;
private NotifyIcon _trayIcon;
public bool KeyHeld { private get; set; }
public KeyWrapper CurrentKey { get; private set; }
protected Accents Accents { get; set; }
protected int CharacterSelectionIndex
{
get
{
return GetNextCharacterIndex();
}
set
{
if (value == 0)
{
_characterSelectionIndex = 0;
}
}
}
public Form1()
{
InitializeNotifyIcon();
InitializeComponent();
InitializeAccents();
InitializeKeyHandler();
InitializeCharacterSelections();
ListenForKeyReleases();
}
private void InitializeAccents()
{
Accents = new Accents();
}
private void InitializeNotifyIcon()
{
_trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenu = new ContextMenu(new MenuItem[]
{
new MenuItem("Configure Character Choices", ConfigureCharacters),
new MenuItem("Help", Help),
new MenuItem("Exit", Exit)
}),
Visible = true,
Text = "Quiccent - Quick accent typing! Right click for more options.",
};
_trayIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_DoubleClick);
}
private void ListenForKeyReleases()
{
Task.Run(() => new AsyncKeyStateHandler(this).CheckLastKeyStillPressed());
}
private int GetNextCharacterIndex()
{
if (_characterSelectionIndex >= (CurrentKey.CharacterSelections?.Count() ?? 0))
{
_characterSelectionIndex = 0;
return _characterSelectionIndex++;
}
return _characterSelectionIndex++;
}
private void InitializeCharacterSelections()
{
CurrentKey = new KeyWrapper(Accents, Keys.None);
}
private void InitializeKeyHandler()
{
var keyHandler = new KeyHandler(Keys.Oemtilde, this);
// ID of 0 for just tilde key.
keyHandler.Register(0, Constants.Constants.JustTilde);
// ID of 1 for tilde + shift.
keyHandler.Register(4, Constants.Constants.TildeAndShift);
}
private void HandleKeypress(bool shiftPressed)
{
SetCurrentKey(shiftPressed);
if (CurrentKey.Key == Keys.None)
{
return;
}
if (!KeyHeld)
{
ResetBecauseKeyReleased();
}
PostKeyToActiveWindow();
}
private void SetCurrentKey(bool shiftPressed)
{
var potentialAccentKey = Constants.Constants.PossibleAccentedKeys.FirstOrDefault(kvp => KeyboardHandler.IsKeyDown(kvp.Key)).Key;
var shouldAddCaps = shiftPressed || Keyboard.IsKeyToggled(Key.CapsLock);
CurrentKey.Set(potentialAccentKey, shouldAddCaps);
}
private void ResetBecauseKeyReleased()
{
CharacterSelectionIndex = 0;
KeyHeld = true;
}
protected void PostKeyToActiveWindow()
{
var keyboardSim = new InputSimulator();
keyboardSim.Keyboard.KeyDown(VirtualKeyCode.BACK);
// We may potentially need this but from my testing, input is very consistent and in order.
// sim.Keyboard.Sleep(1);
var nextMatchingCharacterText = CurrentKey.CharacterSelections[CharacterSelectionIndex];
keyboardSim.Keyboard.TextEntry(nextMatchingCharacterText);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == Constants.Constants.WM_HOTKEY_MSG_ID)
{
HandleKeypress(m.WParam.ToInt32() == 1);
}
base.WndProc(ref m);
}
protected void ConfigureCharacters(object sender, EventArgs e)
{
var configureCharacters = new ConfigureCharacters(Accents);
configureCharacters.Show();
}
protected void Help(object sender, EventArgs e)
{
var help = new Help();
help.Show();
}
protected void Exit(object sender, EventArgs e)
{
_trayIcon.Visible = false;
Application.Exit();
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
ConfigureCharacters(null, null);
}
}
}