-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
406 lines (353 loc) · 13.6 KB
/
Copy pathMainForm.cs
File metadata and controls
406 lines (353 loc) · 13.6 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
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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace KeystrokePaster
{
public partial class MainForm : Form
{
private GlobalHotkey hotkey;
private GlobalHotkey clipboardHotkey;
private KeystrokeSender keystrokeSender;
private bool isTyping = false;
// Settings
public Keys HotkeyKey { get; set; } = Keys.F1;
public Keys HotkeyModifier { get; set; } = Keys.Control;
public Keys ClipboardHotkeyKey { get; set; } = Keys.F2;
public Keys ClipboardHotkeyModifier { get; set; } = Keys.Control;
public int KeystrokeDelay { get; set; } = 10; // milliseconds
public bool LaunchOnStartup { get; set; } = false;
private const int HOTKEY_RELEASE_DELAY = 200; // ms to wait after hotkey
private const string SETTINGS_KEY = @"SOFTWARE\KeystrokePaster";
public MainForm()
{
InitializeComponent();
// Load icon from the executable itself
try
{
this.Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
}
catch
{
// If icon fails to load, just use default
}
InitializeTrayIcon();
keystrokeSender = new KeystrokeSender();
LoadSettings();
LoadStartupSetting();
RegisterHotkey();
UpdateInstructionsText();
}
private void LoadSettings()
{
try
{
using (Microsoft.Win32.RegistryKey key =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(SETTINGS_KEY, false))
{
// No key yet means this is a first run - the defaults stand
if (key == null)
return;
Keys boxModifier = ReadKey(key, "HotkeyModifier", HotkeyModifier);
Keys boxKey = ReadKey(key, "HotkeyKey", HotkeyKey);
Keys clipModifier = ReadKey(key, "ClipboardHotkeyModifier", ClipboardHotkeyModifier);
Keys clipKey = ReadKey(key, "ClipboardHotkeyKey", ClipboardHotkeyKey);
// Only take stored hotkeys if they're still combinations the
// settings dialog can produce, and the two don't collide -
// a hand-edited registry shouldn't leave the app with no hotkeys
if (IsValidHotkey(boxModifier, boxKey) &&
IsValidHotkey(clipModifier, clipKey) &&
!(boxModifier == clipModifier && boxKey == clipKey))
{
HotkeyModifier = boxModifier;
HotkeyKey = boxKey;
ClipboardHotkeyModifier = clipModifier;
ClipboardHotkeyKey = clipKey;
}
if (key.GetValue("KeystrokeDelay") is int delay && delay >= 0 && delay <= 1000)
KeystrokeDelay = delay;
}
}
catch
{
// Unreadable settings - carry on with the defaults
}
}
private Keys ReadKey(Microsoft.Win32.RegistryKey key, string name, Keys fallback)
{
return (key.GetValue(name) is int value) ? (Keys)value : fallback;
}
private bool IsValidHotkey(Keys modifier, Keys key)
{
if (key < Keys.F1 || key > Keys.F12)
return false;
return modifier == Keys.Control
|| modifier == Keys.Alt
|| modifier == Keys.Shift
|| modifier == (Keys.Control | Keys.Alt)
|| modifier == (Keys.Control | Keys.Shift);
}
public void SaveSettings()
{
try
{
using (Microsoft.Win32.RegistryKey key =
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(SETTINGS_KEY))
{
key.SetValue("HotkeyModifier", (int)HotkeyModifier, Microsoft.Win32.RegistryValueKind.DWord);
key.SetValue("HotkeyKey", (int)HotkeyKey, Microsoft.Win32.RegistryValueKind.DWord);
key.SetValue("ClipboardHotkeyModifier", (int)ClipboardHotkeyModifier, Microsoft.Win32.RegistryValueKind.DWord);
key.SetValue("ClipboardHotkeyKey", (int)ClipboardHotkeyKey, Microsoft.Win32.RegistryValueKind.DWord);
key.SetValue("KeystrokeDelay", KeystrokeDelay, Microsoft.Win32.RegistryValueKind.DWord);
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save settings: {ex.Message}\n\nThey'll apply now but won't survive a restart.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void UpdateInstructionsText()
{
string boxCombo = $"{GetModifierName(HotkeyModifier)}+{HotkeyKey}";
string clipCombo = $"{GetModifierName(ClipboardHotkeyModifier)}+{ClipboardHotkeyKey}";
lblInstructions.Text =
$"{boxCombo} in target window types the box above" + Environment.NewLine +
$"{clipCombo} types the clipboard directly";
}
private string GetModifierName(Keys modifier)
{
if (modifier == Keys.Control)
return "Ctrl";
else if (modifier == Keys.Alt)
return "Alt";
else if (modifier == Keys.Shift)
return "Shift";
else if (modifier == (Keys.Control | Keys.Alt))
return "Ctrl+Alt";
else if (modifier == (Keys.Control | Keys.Shift))
return "Ctrl+Shift";
else
return modifier.ToString();
}
private void LoadStartupSetting()
{
try
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
object value = key?.GetValue("KeystrokePaster");
LaunchOnStartup = (value != null);
key?.Close();
}
catch
{
LaunchOnStartup = false;
}
}
private void InitializeTrayIcon()
{
trayIcon = new NotifyIcon
{
Icon = this.Icon, // Use the form's icon (which should be set from embedded resource)
Text = "Keystroke Paster",
Visible = true
};
trayIcon.DoubleClick += (s, e) =>
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Activate();
};
// Context menu for tray icon
ContextMenuStrip trayMenu = new ContextMenuStrip();
trayMenu.Items.Add("Show", null, (s, e) =>
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Activate();
});
trayMenu.Items.Add("Exit", null, (s, e) => Application.Exit());
trayIcon.ContextMenuStrip = trayMenu;
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
// Don't hide - just minimize to keep window handle active
this.ShowInTaskbar = false;
// Balloon tip removed - no notification when minimizing
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
private void lblStatus_Click(object sender, EventArgs e)
{
// Empty event handler - added by designer
}
private void BtnSettings_Click(object sender, EventArgs e)
{
// Temporarily disable TopMost so settings dialog shows on top
this.TopMost = false;
using (SettingsForm settingsForm = new SettingsForm(this))
{
if (settingsForm.ShowDialog() == DialogResult.OK)
{
// Remember the new settings for next launch
SaveSettings();
// Re-register hotkey with new settings
RegisterHotkey();
// Update the instructions text to show new hotkey
UpdateInstructionsText();
}
}
// Re-enable TopMost
this.TopMost = true;
}
private void RegisterHotkey()
{
// Unregister old hotkeys first
ReleaseHotkey(ref hotkey);
ReleaseHotkey(ref clipboardHotkey);
// Small delay to ensure cleanup
System.Threading.Thread.Sleep(100);
// Register new hotkeys
try
{
hotkey = new GlobalHotkey(HotkeyModifier, HotkeyKey, this, GlobalHotkey.HOTKEY_ID);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to register text box hotkey: {ex.Message}\n\nTry a different hotkey combination.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
clipboardHotkey = new GlobalHotkey(ClipboardHotkeyModifier, ClipboardHotkeyKey, this, GlobalHotkey.HOTKEY_ID_CLIPBOARD);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to register clipboard hotkey: {ex.Message}\n\nTry a different hotkey combination.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ReleaseHotkey(ref GlobalHotkey target)
{
if (target == null)
return;
try
{
target.Unregister();
target.Dispose();
}
catch { }
target = null;
}
private void Hotkey_Pressed(object sender, EventArgs e)
{
TypeText(txtPasteBox.Text);
}
private void ClipboardHotkey_Pressed(object sender, EventArgs e)
{
if (isTyping)
return;
string clipboardText = GetClipboardText();
if (string.IsNullOrEmpty(clipboardText))
{
UpdateStatus("Clipboard is empty (or has no text)", Color.Red);
return;
}
TypeText(clipboardText);
}
private string GetClipboardText()
{
// The clipboard is a shared resource - another app may have it locked,
// so give it a couple of tries before giving up.
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
return Clipboard.ContainsText() ? Clipboard.GetText() : null;
}
catch
{
System.Threading.Thread.Sleep(50);
}
}
return null;
}
private async void TypeText(string text)
{
if (isTyping || string.IsNullOrEmpty(text))
return;
isTyping = true;
UpdateStatus("Waiting for hotkey release...", Color.Orange);
// Wait for hotkey to be released
await Task.Delay(HOTKEY_RELEASE_DELAY);
UpdateStatus("Typing...", Color.Green);
try
{
await Task.Run(() =>
{
keystrokeSender.SendText(text, KeystrokeDelay);
});
UpdateStatus("Done!", Color.Blue);
await Task.Delay(2000);
UpdateStatus("Waiting", Color.Black);
}
catch (Exception ex)
{
UpdateStatus($"Error: {ex.Message}", Color.Red);
}
finally
{
isTyping = false;
}
}
private void UpdateStatus(string message, Color color)
{
if (lblStatus.InvokeRequired)
{
lblStatus.Invoke(new Action(() =>
{
lblStatus.Text = $"Status: {message}";
lblStatus.ForeColor = color;
}));
}
else
{
lblStatus.Text = $"Status: {message}";
lblStatus.ForeColor = color;
}
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// Re-register hotkey with new handle
if (hotkey != null)
{
RegisterHotkey();
}
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
if (m.Msg == WM_HOTKEY)
{
// WParam carries the id we registered the hotkey under
int id = m.WParam.ToInt32();
if (id == GlobalHotkey.HOTKEY_ID)
Hotkey_Pressed(this, EventArgs.Empty);
else if (id == GlobalHotkey.HOTKEY_ID_CLIPBOARD)
ClipboardHotkey_Pressed(this, EventArgs.Empty);
}
base.WndProc(ref m);
}
}
}