-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
316 lines (283 loc) · 10.9 KB
/
Copy pathSettingsForm.cs
File metadata and controls
316 lines (283 loc) · 10.9 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
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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace KeystrokePaster
{
public partial class SettingsForm : Form
{
private MainForm mainForm;
private ComboBox cmbModifier;
private ComboBox cmbKey;
private ComboBox cmbClipModifier;
private ComboBox cmbClipKey;
private NumericUpDown numDelay;
private CheckBox chkLaunchOnStartup;
private Button btnOK;
private Button btnCancel;
public SettingsForm(MainForm parent)
{
mainForm = parent;
InitializeComponent();
LoadSettings();
}
private void InitializeComponent()
{
this.Text = "Settings";
// ClientSize, not Size - control positions below are client coordinates,
// and the title bar height varies enough to clip the buttons otherwise
this.ClientSize = new Size(334, 272);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
// Hotkey label
Label lblHotkey = new Label
{
Text = "Text box:",
Location = new Point(20, 25),
Size = new Size(80, 20),
Font = new Font("Segoe UI", 9F)
};
this.Controls.Add(lblHotkey);
// Modifier dropdown
cmbModifier = new ComboBox
{
Location = new Point(100, 23),
Size = new Size(90, 25),
DropDownStyle = ComboBoxStyle.DropDownList
};
cmbModifier.Items.AddRange(new object[] { "Ctrl", "Alt", "Shift", "Ctrl+Alt", "Ctrl+Shift" });
this.Controls.Add(cmbModifier);
// Plus label
Label lblPlus = new Label
{
Text = "+",
Location = new Point(195, 25),
Size = new Size(15, 20),
Font = new Font("Segoe UI", 9F, FontStyle.Bold)
};
this.Controls.Add(lblPlus);
// Key dropdown
cmbKey = new ComboBox
{
Location = new Point(215, 23),
Size = new Size(100, 25),
DropDownStyle = ComboBoxStyle.DropDownList
};
cmbKey.Items.AddRange(new object[] {
"F1", "F2", "F3", "F4", "F5", "F6",
"F7", "F8", "F9", "F10", "F11", "F12"
});
this.Controls.Add(cmbKey);
// Clipboard hotkey label
Label lblClipHotkey = new Label
{
Text = "Clipboard:",
Location = new Point(20, 60),
Size = new Size(80, 20),
Font = new Font("Segoe UI", 9F)
};
this.Controls.Add(lblClipHotkey);
// Clipboard modifier dropdown
cmbClipModifier = new ComboBox
{
Location = new Point(100, 58),
Size = new Size(90, 25),
DropDownStyle = ComboBoxStyle.DropDownList
};
cmbClipModifier.Items.AddRange(new object[] { "Ctrl", "Alt", "Shift", "Ctrl+Alt", "Ctrl+Shift" });
this.Controls.Add(cmbClipModifier);
// Plus label
Label lblClipPlus = new Label
{
Text = "+",
Location = new Point(195, 60),
Size = new Size(15, 20),
Font = new Font("Segoe UI", 9F, FontStyle.Bold)
};
this.Controls.Add(lblClipPlus);
// Clipboard key dropdown
cmbClipKey = new ComboBox
{
Location = new Point(215, 58),
Size = new Size(100, 25),
DropDownStyle = ComboBoxStyle.DropDownList
};
cmbClipKey.Items.AddRange(new object[] {
"F1", "F2", "F3", "F4", "F5", "F6",
"F7", "F8", "F9", "F10", "F11", "F12"
});
this.Controls.Add(cmbClipKey);
// Clipboard description
Label lblClipDesc = new Label
{
Text = "Types the clipboard directly - nothing to paste in the box first",
Location = new Point(20, 85),
Size = new Size(300, 30),
Font = new Font("Segoe UI", 7.5F),
ForeColor = Color.Gray
};
this.Controls.Add(lblClipDesc);
// Delay label
Label lblDelay = new Label
{
Text = "Keystroke Delay (ms):",
Location = new Point(20, 122),
Size = new Size(140, 20),
Font = new Font("Segoe UI", 9F)
};
this.Controls.Add(lblDelay);
// Delay numeric input
numDelay = new NumericUpDown
{
Location = new Point(165, 120),
Size = new Size(80, 25),
Minimum = 0,
Maximum = 1000,
Increment = 5,
Value = 10
};
this.Controls.Add(numDelay);
// Delay description
Label lblDelayDesc = new Label
{
Text = "Time to wait between each keystroke\n(0 = fastest, 50+ = safer for slow systems)",
Location = new Point(20, 152),
Size = new Size(300, 35),
Font = new Font("Segoe UI", 7.5F),
ForeColor = Color.Gray
};
this.Controls.Add(lblDelayDesc);
// Launch on startup checkbox
chkLaunchOnStartup = new CheckBox
{
Text = "Launch on Windows startup",
Location = new Point(20, 197),
Size = new Size(200, 20),
Font = new Font("Segoe UI", 9F)
};
this.Controls.Add(chkLaunchOnStartup);
// OK button
btnOK = new Button
{
Text = "OK",
Location = new Point(150, 227),
Size = new Size(75, 30),
DialogResult = DialogResult.OK
};
btnOK.Click += BtnOK_Click;
this.Controls.Add(btnOK);
// Cancel button
btnCancel = new Button
{
Text = "Cancel",
Location = new Point(240, 227),
Size = new Size(75, 30),
DialogResult = DialogResult.Cancel
};
this.Controls.Add(btnCancel);
this.AcceptButton = btnOK;
this.CancelButton = btnCancel;
}
private void LoadSettings()
{
// Load hotkeys
LoadHotkey(cmbModifier, cmbKey, mainForm.HotkeyModifier, mainForm.HotkeyKey);
LoadHotkey(cmbClipModifier, cmbClipKey, mainForm.ClipboardHotkeyModifier, mainForm.ClipboardHotkeyKey);
// Load delay
numDelay.Value = mainForm.KeystrokeDelay;
// Load launch on startup
chkLaunchOnStartup.Checked = mainForm.LaunchOnStartup;
}
private void LoadHotkey(ComboBox modifierBox, ComboBox keyBox, Keys modifier, Keys key)
{
// Load modifier
if (modifier == Keys.Control)
modifierBox.SelectedIndex = 0; // Ctrl
else if (modifier == Keys.Alt)
modifierBox.SelectedIndex = 1; // Alt
else if (modifier == Keys.Shift)
modifierBox.SelectedIndex = 2; // Shift
else if (modifier == (Keys.Control | Keys.Alt))
modifierBox.SelectedIndex = 3; // Ctrl+Alt
else if (modifier == (Keys.Control | Keys.Shift))
modifierBox.SelectedIndex = 4; // Ctrl+Shift
else
modifierBox.SelectedIndex = 0; // Default to Ctrl
// Load key
if (key >= Keys.F1 && key <= Keys.F12)
{
int fKeyNumber = key - Keys.F1;
keyBox.SelectedIndex = fKeyNumber;
}
else
{
keyBox.SelectedIndex = 0; // Default to F1
}
}
private Keys GetSelectedModifier(ComboBox modifierBox)
{
switch (modifierBox.SelectedIndex)
{
case 1: return Keys.Alt;
case 2: return Keys.Shift;
case 3: return Keys.Control | Keys.Alt;
case 4: return Keys.Control | Keys.Shift;
default: return Keys.Control;
}
}
private void BtnOK_Click(object sender, EventArgs e)
{
// Both hotkeys have to be distinct - Windows won't register the same
// combination twice, so catch it here rather than failing silently later
if (cmbModifier.SelectedIndex == cmbClipModifier.SelectedIndex &&
cmbKey.SelectedIndex == cmbClipKey.SelectedIndex)
{
MessageBox.Show("The text box hotkey and the clipboard hotkey must be different.",
"Duplicate Hotkey", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.DialogResult = DialogResult.None;
return;
}
// Save hotkeys
mainForm.HotkeyModifier = GetSelectedModifier(cmbModifier);
mainForm.HotkeyKey = Keys.F1 + cmbKey.SelectedIndex;
mainForm.ClipboardHotkeyModifier = GetSelectedModifier(cmbClipModifier);
mainForm.ClipboardHotkeyKey = Keys.F1 + cmbClipKey.SelectedIndex;
// Save delay
mainForm.KeystrokeDelay = (int)numDelay.Value;
// Save launch on startup
bool wasEnabled = mainForm.LaunchOnStartup;
mainForm.LaunchOnStartup = chkLaunchOnStartup.Checked;
// Update Windows registry for startup
if (mainForm.LaunchOnStartup != wasEnabled)
{
SetStartup(mainForm.LaunchOnStartup);
}
}
private void SetStartup(bool enable)
{
try
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (enable)
{
// Add to startup
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
key.SetValue("KeystrokePaster", exePath);
}
else
{
// Remove from startup
key.DeleteValue("KeystrokePaster", false);
}
key.Close();
}
catch (Exception ex)
{
MessageBox.Show($"Failed to update startup settings: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}