Repository navigation
Expand file tree
/
Copy pathWindowsInput.cs
More file actions
398 lines (363 loc) · 13.7 KB
/
Copy pathWindowsInput.cs
File metadata and controls
398 lines (363 loc) · 13.7 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyParcel
{
internal static class PasteShortcut
{
internal const string Phone = "phone", Clipboard = "clipboard", Resume = "resume";
}
internal sealed class Hotkeys : NativeWindow, IDisposable
{
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr window, int id, uint modifiers, uint key);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr window, int id);
private sealed class Binding { internal Keys Chord; internal int Id; }
private readonly Dictionary<string, Binding> bindings = new Dictionary<string, Binding>();
private readonly Action<string> pressed;
private const int ProbeId = 0xbfff;
private int nextId = 1;
internal Hotkeys(Action<string> pressed)
{
this.pressed = pressed;
CreateHandle(new CreateParams { Parent = new IntPtr(-3) });
}
internal string Set(string shortcut, Keys chord, string savePath = null)
{
if (!Valid(chord)) return "请用 Ctrl、Alt 或 Shift 搭配字母、数字或功能键。Ctrl + V 留给系统。";
Binding existing;
if (bindings.TryGetValue(shortcut, out existing) && existing.Chord == chord && existing.Id != 0) return null;
int id = InUse(chord, shortcut) ? 0 : Register(chord);
if (id == 0)
return "快捷键已被占用。请录制另一组。";
try
{
if (savePath != null)
MobileInbox.SaveSetting(savePath, ((int)chord).ToString(CultureInfo.InvariantCulture));
}
catch (IOException)
{
UnregisterHotKey(Handle, id);
return "无法保存设置。请检查用户设置目录。";
}
catch (UnauthorizedAccessException)
{
UnregisterHotKey(Handle, id);
return "无法保存设置。请检查用户设置目录。";
}
Remove(shortcut);
bindings[shortcut] = new Binding { Chord = chord, Id = id };
return null;
}
internal bool InUse(Keys chord, string except = null)
{
return bindings.Any(item => item.Key != except && item.Value.Chord == chord);
}
internal bool CanRegister(Keys chord, string except = null)
{
if (!Valid(chord) || InUse(chord, except)) return false;
Binding existing;
if (except != null && bindings.TryGetValue(except, out existing) &&
existing.Id != 0 && existing.Chord == chord) return true;
if (!RegisterHotKey(Handle, ProbeId, Modifiers(chord), (uint)(chord & Keys.KeyCode))) return false;
UnregisterHotKey(Handle, ProbeId);
return true;
}
internal void Remove(string shortcut)
{
Binding binding;
if (!bindings.TryGetValue(shortcut, out binding)) return;
if (binding.Id != 0) UnregisterHotKey(Handle, binding.Id);
bindings.Remove(shortcut);
}
internal void Suspend()
{
foreach (var binding in bindings.Values)
{
if (binding.Id != 0) UnregisterHotKey(Handle, binding.Id);
binding.Id = 0;
}
}
internal int Restore()
{
int failed = 0;
foreach (var binding in bindings.Values)
{
if (binding.Id != 0) continue;
binding.Id = Register(binding.Chord);
if (binding.Id == 0) failed++;
}
return failed;
}
private int Register(Keys chord)
{
for (int attempt = 0; attempt < ProbeId - 1; attempt++)
{
int id = nextId++;
if (nextId == ProbeId) nextId = 1;
if (bindings.Values.Any(binding => binding.Id == id)) continue;
return RegisterHotKey(Handle, id, Modifiers(chord), (uint)(chord & Keys.KeyCode)) ? id : 0;
}
return 0;
}
private static uint Modifiers(Keys chord)
{
uint mods = 0x4000; // MOD_NOREPEAT
if ((chord & Keys.Alt) != 0) mods |= 1;
if ((chord & Keys.Control) != 0) mods |= 2;
if ((chord & Keys.Shift) != 0) mods |= 4;
return mods;
}
internal static bool Valid(Keys chord)
{
Keys key = chord & Keys.KeyCode, mods = chord & Keys.Modifiers;
return mods != Keys.None && (mods & ~(Keys.Control | Keys.Shift | Keys.Alt)) == 0 &&
chord != (Keys.Control | Keys.V) &&
(key >= Keys.A && key <= Keys.Z || key >= Keys.D0 && key <= Keys.D9 || key >= Keys.F1 && key <= Keys.F12);
}
internal static string Display(Keys chord)
{
if (chord == Keys.None) return "未分配";
Keys key = chord & Keys.KeyCode;
return ((chord & Keys.Control) != 0 ? "Ctrl + " : "") +
((chord & Keys.Alt) != 0 ? "Alt + " : "") +
((chord & Keys.Shift) != 0 ? "Shift + " : "") +
(key >= Keys.D0 && key <= Keys.D9 ? (key - Keys.D0).ToString() : key.ToString());
}
protected override void WndProc(ref Message message)
{
if (message.Msg == 0x0312)
{
int id = message.WParam.ToInt32();
if (id != 0)
{
string shortcut = bindings.FirstOrDefault(item => item.Value.Id == id).Key;
if (shortcut != null) { pressed(shortcut); return; }
}
}
base.WndProc(ref message);
}
public void Dispose()
{
if (Handle == IntPtr.Zero) return;
Suspend();
DestroyHandle();
}
}
internal sealed class PacedInput : IDisposable
{
private readonly Timer timer = new Timer();
private readonly Action<string> status;
private readonly Func<IntPtr> foreground;
private readonly Action<string> defaultSend;
private Action<string> send;
private RimeInput rime;
private bool rimeStarted;
private string pending;
private IntPtr target;
private int position, interval;
private string source;
private bool running;
internal PacedInput(Action<string> status, Func<IntPtr> foreground, Action<string> send)
{
this.status = status;
this.foreground = foreground;
defaultSend = send;
timer.Tick += Tick;
}
internal bool Begin(string shortcut, string text, int delay, IntPtr window, Action<string> sendOverride = null, RimeInput rimeInput = null)
{
if (running || String.IsNullOrEmpty(text) || window == IntPtr.Zero) return false;
send = sendOverride ?? defaultSend;
rime = rimeInput;
target = window;
source = shortcut;
pending = text;
position = 0;
interval = delay;
StartTimer("正在输入。再按同一个粘贴快捷键可暂停。");
return true;
}
internal bool HandleRunningPasteShortcut(string shortcut)
{
if (!running) return false;
if (shortcut == source) Pause("已暂停。按「继续粘贴」从原位置接着输入。");
return true;
}
internal bool Resume()
{
if (running) return false;
if (pending == null)
{
status("没有可继续的粘贴。");
return false;
}
if (foreground() != target)
{
status("请回到原窗口,再按「继续粘贴」。");
return false;
}
StartTimer("继续输入剩余文字。");
return true;
}
private void StartTimer(string message)
{
running = true;
timer.Interval = 500;
timer.Start();
status(message);
}
private void Pause(string message)
{
timer.Stop();
running = false;
status(message + StopRime());
}
private void Finish(string message)
{
string warning = StopRime();
Discard();
status(warning.Length == 0 ? message : "输入失败。" + warning);
}
internal bool Discard()
{
bool hadPending = pending != null;
timer.Stop();
running = false;
string warning = StopRime();
if (warning.Length != 0) status("输入失败。" + warning);
pending = null;
send = null;
rime = null;
target = IntPtr.Zero;
return hadPending;
}
private string StopRime()
{
if (!rimeStarted) return "";
rimeStarted = false;
try { rime.Stop(); return ""; }
catch (Exception error) { return " " + error.Message; }
}
private void Tick(object sender, EventArgs args)
{
timer.Stop();
if (!running) return;
try
{
if (position == pending.Length)
{
Finish("本次输入已完成。");
return;
}
int count = interval == 0 && rime == null ? 128 : 1;
while (count-- > 0 && position < pending.Length)
{
if (foreground() != target)
{
Pause("已切换窗口,输入已暂停。回到原窗口后可继续。");
return;
}
if (rime != null && !rimeStarted)
{
rimeStarted = true;
rime.Start();
}
int start = position;
int next = start + 1;
char c = pending[start];
if (c == '\r' || c == '\n')
{
if (c == '\r' && next < pending.Length && pending[next] == '\n') next++;
send("\n");
}
else
{
if (Char.IsHighSurrogate(c) && next < pending.Length && Char.IsLowSurrogate(pending[next]))
next++;
send(pending.Substring(start, next - start));
}
position = next;
}
if (position == pending.Length && rime == null) Finish("本次输入已完成。");
else
{
// The IME acknowledges composition before Windows inserts the final character.
timer.Interval = position == pending.Length ? 50 : Math.Max(interval, 1);
timer.Start();
}
}
catch (RimeFocusLostException)
{
Pause("已切换窗口,输入已暂停。回到原窗口后可继续。");
}
catch (Exception ex)
{
Finish("输入失败。 " + ex.Message);
}
}
public void Dispose() { Discard(); timer.Dispose(); }
}
internal static class WindowsKeyboard
{
[DllImport("user32.dll")]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint count, Input[] inputs, int size);
[StructLayout(LayoutKind.Sequential)]
internal struct Input
{
public uint Type;
public InputData Data;
}
[StructLayout(LayoutKind.Explicit)]
internal struct InputData
{
[FieldOffset(0)] public KeyboardData Keyboard;
[FieldOffset(0)] public MouseData Mouse;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KeyboardData
{
public ushort Key, Scan;
public uint Flags, Time;
public UIntPtr Extra;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MouseData
{
public int X, Y;
public uint Data, Flags, Time;
public UIntPtr Extra;
}
internal static Input[] Encode(string unit)
{
var events = new Input[unit.Length * 2];
bool newline = unit == "\n";
for (int i = 0; i < unit.Length; i++)
{
var key = new KeyboardData
{
Key = (ushort)(newline ? 13 : 0),
Scan = (ushort)(newline ? 0 : unit[i]),
Flags = newline ? 0u : 4u
};
events[i * 2] = new Input { Type = 1, Data = new InputData { Keyboard = key } };
key.Flags |= 2;
events[i * 2 + 1] = new Input { Type = 1, Data = new InputData { Keyboard = key } };
}
return events;
}
internal static void Send(string unit)
{
var events = Encode(unit);
if (SendInput((uint)events.Length, events, Marshal.SizeOf(typeof(Input))) != events.Length)
throw new InvalidOperationException("请检查目标窗口是否允许模拟键盘输入。");
}
}
}