@copylee/dsh-computer-use 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/helper/Capture.cs +682 -0
- package/helper/Input.cs +500 -0
- package/helper/Native.cs +136 -0
- package/helper/Overlay.cs +1685 -0
- package/helper/Program.cs +244 -0
- package/helper/Uia.cs +882 -0
- package/helper/Windows.cs +761 -0
- package/lib/client.js +15 -3
- package/lib/index.js +801 -364
- package/lib/types/access.d.ts +7 -0
- package/lib/types/cdp.d.ts +14 -3
- package/lib/types/computer.d.ts +15 -3
- package/lib/types/diagnostics.d.ts +1 -0
- package/lib/types/helper-client.d.ts +4 -2
- package/lib/types/index.d.ts +1 -0
- package/lib/types/lease.d.ts +30 -0
- package/lib/types/ui-diff.d.ts +28 -0
- package/package.json +3 -2
- package/helper/CuHelper.cs +0 -4749
package/helper/Input.cs
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
// Mouse/keyboard SendInput synthesis, eased cursor glide, clipboard manager, and pass-through guard (C# 5).
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections;
|
|
5
|
+
using System.Collections.Generic;
|
|
6
|
+
using System.Diagnostics;
|
|
7
|
+
using System.Drawing;
|
|
8
|
+
using System.Drawing.Drawing2D;
|
|
9
|
+
using System.Drawing.Imaging;
|
|
10
|
+
using System.Drawing.Text;
|
|
11
|
+
using System.IO;
|
|
12
|
+
using System.Reflection;
|
|
13
|
+
using System.Runtime.InteropServices;
|
|
14
|
+
using System.Text;
|
|
15
|
+
using System.Threading;
|
|
16
|
+
using System.Web.Script.Serialization;
|
|
17
|
+
using System.Windows.Automation;
|
|
18
|
+
using System.Windows.Forms;
|
|
19
|
+
using Microsoft.Win32;
|
|
20
|
+
|
|
21
|
+
namespace DshComputerUse
|
|
22
|
+
{
|
|
23
|
+
internal static class Input
|
|
24
|
+
{
|
|
25
|
+
static readonly int InputSize = Marshal.SizeOf(typeof(Native.INPUT));
|
|
26
|
+
static readonly HashSet<int> Extended = new HashSet<int> { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x2C, 0x2D, 0x2E, 0x5B, 0x5C, 0x5D, 0x6F, 0x90, 0xA3, 0xA5 };
|
|
27
|
+
|
|
28
|
+
static Native.INPUT Mouse(uint flags, uint data)
|
|
29
|
+
{
|
|
30
|
+
var i = new Native.INPUT(); i.type = Native.INPUT_MOUSE;
|
|
31
|
+
i.u.mi.dwFlags = flags; i.u.mi.mouseData = data; i.u.mi.dwExtraInfo = Native.Marker;
|
|
32
|
+
return i;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static Native.INPUT Key(int vk, bool up)
|
|
36
|
+
{
|
|
37
|
+
var i = new Native.INPUT(); i.type = Native.INPUT_KEYBOARD;
|
|
38
|
+
i.u.ki.wVk = (ushort)vk;
|
|
39
|
+
i.u.ki.wScan = (ushort)Native.MapVirtualKey((uint)vk, 0);
|
|
40
|
+
i.u.ki.dwFlags = (up ? Native.KEYEVENTF_KEYUP : 0) | (Extended.Contains(vk) ? Native.KEYEVENTF_EXTENDEDKEY : 0);
|
|
41
|
+
i.u.ki.dwExtraInfo = Native.Marker;
|
|
42
|
+
return i;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static Native.INPUT Unicode(char c, bool up)
|
|
46
|
+
{
|
|
47
|
+
var i = new Native.INPUT(); i.type = Native.INPUT_KEYBOARD;
|
|
48
|
+
i.u.ki.wVk = 0; i.u.ki.wScan = c;
|
|
49
|
+
i.u.ki.dwFlags = Native.KEYEVENTF_UNICODE | (up ? Native.KEYEVENTF_KEYUP : 0);
|
|
50
|
+
i.u.ki.dwExtraInfo = Native.Marker;
|
|
51
|
+
return i;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static void Send(List<Native.INPUT> inputs)
|
|
55
|
+
{
|
|
56
|
+
if (inputs.Count == 0) return;
|
|
57
|
+
WaitForInstructionBox();
|
|
58
|
+
uint sent = Native.SendInput((uint)inputs.Count, inputs.ToArray(), InputSize);
|
|
59
|
+
if (sent != inputs.Count)
|
|
60
|
+
throw new Exception("SendInput was blocked (error " + Marshal.GetLastWin32Error() + "). The target may be running elevated (as administrator) or the secure desktop (UAC / lock screen) is active.");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The user opened the progress card's instruction box mid-action (say,
|
|
65
|
+
* halfway through typing): hold the rest until it closes and focus is
|
|
66
|
+
* back, so no keystroke lands in the box.
|
|
67
|
+
*/
|
|
68
|
+
static void WaitForInstructionBox()
|
|
69
|
+
{
|
|
70
|
+
if (Overlay.InputHwnd == IntPtr.Zero) return;
|
|
71
|
+
var until = DateTime.UtcNow.AddSeconds(90);
|
|
72
|
+
while (Overlay.InputHwnd != IntPtr.Zero)
|
|
73
|
+
{
|
|
74
|
+
if (DateTime.UtcNow > until) throw new Exception("Stopped: the user is typing an instruction to you in the progress card.");
|
|
75
|
+
Thread.Sleep(100);
|
|
76
|
+
}
|
|
77
|
+
// The box hands the focus back to the window it took it from: wait until that happened.
|
|
78
|
+
IntPtr target = Overlay.InputReturnsTo;
|
|
79
|
+
var back = DateTime.UtcNow.AddSeconds(4);
|
|
80
|
+
while (target != IntPtr.Zero && Native.IsWindow(target) && Native.GetForegroundWindow() != target && DateTime.UtcNow < back) Thread.Sleep(50);
|
|
81
|
+
Thread.Sleep(150);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public static void Move(int x, int y)
|
|
85
|
+
{
|
|
86
|
+
Native.SetCursorPos(x, y);
|
|
87
|
+
// A zero relative move lets apps that only watch WM_MOUSEMOVE notice the cursor.
|
|
88
|
+
Send(new List<Native.INPUT> { Mouse(Native.MOUSEEVENTF_MOVE, 0) });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static void Flags(string button, out uint down, out uint up)
|
|
92
|
+
{
|
|
93
|
+
switch (button)
|
|
94
|
+
{
|
|
95
|
+
case "right": down = Native.MOUSEEVENTF_RIGHTDOWN; up = Native.MOUSEEVENTF_RIGHTUP; break;
|
|
96
|
+
case "middle": down = Native.MOUSEEVENTF_MIDDLEDOWN; up = Native.MOUSEEVENTF_MIDDLEUP; break;
|
|
97
|
+
default: down = Native.MOUSEEVENTF_LEFTDOWN; up = Native.MOUSEEVENTF_LEFTUP; break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static void Modifiers(List<int> vks, bool up)
|
|
102
|
+
{
|
|
103
|
+
var list = new List<Native.INPUT>();
|
|
104
|
+
if (up) for (int i = vks.Count - 1; i >= 0; i--) list.Add(Key(vks[i], true));
|
|
105
|
+
else foreach (var vk in vks) list.Add(Key(vk, false));
|
|
106
|
+
Send(list);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
sealed class ClickRecord
|
|
110
|
+
{
|
|
111
|
+
public int X;
|
|
112
|
+
public int Y;
|
|
113
|
+
public DateTime At;
|
|
114
|
+
}
|
|
115
|
+
static readonly List<ClickRecord> ClickHistory = new List<ClickRecord>();
|
|
116
|
+
static readonly object ClickLock = new object();
|
|
117
|
+
|
|
118
|
+
static int NoteClick(int x, int y)
|
|
119
|
+
{
|
|
120
|
+
var now = DateTime.UtcNow;
|
|
121
|
+
var cutoff = now.AddMinutes(-2);
|
|
122
|
+
lock (ClickLock)
|
|
123
|
+
{
|
|
124
|
+
int removeCount = 0;
|
|
125
|
+
while (removeCount < ClickHistory.Count && ClickHistory[removeCount].At < cutoff) removeCount++;
|
|
126
|
+
if (removeCount > 0) ClickHistory.RemoveRange(0, removeCount);
|
|
127
|
+
var rec = new ClickRecord();
|
|
128
|
+
rec.X = x;
|
|
129
|
+
rec.Y = y;
|
|
130
|
+
rec.At = now;
|
|
131
|
+
ClickHistory.Add(rec);
|
|
132
|
+
int hits = 0;
|
|
133
|
+
for (int i = 0; i < ClickHistory.Count; i++)
|
|
134
|
+
{
|
|
135
|
+
var item = ClickHistory[i];
|
|
136
|
+
if (Math.Abs(item.X - x) <= 24 && Math.Abs(item.Y - y) <= 24) hits++;
|
|
137
|
+
}
|
|
138
|
+
return hits;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public static object Click(Dictionary<string, object> r)
|
|
143
|
+
{
|
|
144
|
+
int x, y;
|
|
145
|
+
Native.POINT savedCursor;
|
|
146
|
+
bool hadSavedCursor = Native.GetCursorPos(out savedCursor);
|
|
147
|
+
bool restorePointer = Args.Bool(r, "restorePointer", false);
|
|
148
|
+
bool moved = Args.Has(r, "x");
|
|
149
|
+
if (moved)
|
|
150
|
+
{
|
|
151
|
+
x = Args.Int(r, "x", 0);
|
|
152
|
+
y = Args.Int(r, "y", 0);
|
|
153
|
+
}
|
|
154
|
+
else
|
|
155
|
+
{
|
|
156
|
+
if (!hadSavedCursor) { x = 0; y = 0; }
|
|
157
|
+
else { x = savedCursor.X; y = savedCursor.Y; }
|
|
158
|
+
}
|
|
159
|
+
string button = Args.Str(r, "button", "left");
|
|
160
|
+
if (button != "right" && button != "middle") button = "left";
|
|
161
|
+
uint down, up; Flags(button, out down, out up);
|
|
162
|
+
int count = Math.Max(1, Math.Min(3, Args.Int(r, "count", 1)));
|
|
163
|
+
var mods = Args.Ints(r, "modifiers");
|
|
164
|
+
string coordLabel = Args.Has(r, "labelX") && Args.Has(r, "labelY")
|
|
165
|
+
? Args.Int(r, "labelX", x) + ", " + Args.Int(r, "labelY", y)
|
|
166
|
+
: x + ", " + y;
|
|
167
|
+
bool showTarget = Overlay.ShowClickTarget;
|
|
168
|
+
if (showTarget)
|
|
169
|
+
{
|
|
170
|
+
string preVerb = count >= 3 ? "三击" : count == 2 ? "双击" : button == "right" ? "右键" : button == "middle" ? "中键" : "点击";
|
|
171
|
+
Overlay.GlideMark(x, y, preVerb + " " + coordLabel, Color.FromArgb(217, 119, 87), 2000, false, false);
|
|
172
|
+
}
|
|
173
|
+
if (moved)
|
|
174
|
+
{
|
|
175
|
+
Overlay.Dodge(x, y);
|
|
176
|
+
Move(x, y);
|
|
177
|
+
}
|
|
178
|
+
if (showTarget)
|
|
179
|
+
{
|
|
180
|
+
Thread.Sleep(70);
|
|
181
|
+
}
|
|
182
|
+
else if (moved)
|
|
183
|
+
{
|
|
184
|
+
Thread.Sleep(30);
|
|
185
|
+
}
|
|
186
|
+
Modifiers(mods, false);
|
|
187
|
+
try
|
|
188
|
+
{
|
|
189
|
+
for (int i = 0; i < count; i++)
|
|
190
|
+
{
|
|
191
|
+
Send(new List<Native.INPUT> { Mouse(down, 0) });
|
|
192
|
+
Thread.Sleep(20);
|
|
193
|
+
Send(new List<Native.INPUT> { Mouse(up, 0) });
|
|
194
|
+
if (i < count - 1) Thread.Sleep(40);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
finally
|
|
198
|
+
{
|
|
199
|
+
Modifiers(mods, true);
|
|
200
|
+
if (restorePointer && moved && hadSavedCursor)
|
|
201
|
+
{
|
|
202
|
+
try { Native.SetCursorPos(savedCursor.X, savedCursor.Y); } catch (Exception) { }
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (showTarget)
|
|
206
|
+
{
|
|
207
|
+
Overlay.MarkResult(x, y, button, count, coordLabel);
|
|
208
|
+
}
|
|
209
|
+
int repeat = NoteClick(x, y);
|
|
210
|
+
var result = new Dictionary<string, object>();
|
|
211
|
+
result["x"] = x;
|
|
212
|
+
result["y"] = y;
|
|
213
|
+
result["button"] = button;
|
|
214
|
+
result["count"] = count;
|
|
215
|
+
result["repeat"] = repeat;
|
|
216
|
+
result["restoredPointer"] = restorePointer && moved && hadSavedCursor;
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public static object Button(Dictionary<string, object> r)
|
|
221
|
+
{
|
|
222
|
+
if (Args.Has(r, "x")) { int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0); Overlay.Dodge(x, y); Move(x, y); Thread.Sleep(20); }
|
|
223
|
+
uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
|
|
224
|
+
Send(new List<Native.INPUT> { Mouse(Args.Bool(r, "up", false) ? up : down, 0) });
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
public static object Drag(Dictionary<string, object> r)
|
|
229
|
+
{
|
|
230
|
+
int x1 = Args.Int(r, "x1", 0), y1 = Args.Int(r, "y1", 0), x2 = Args.Int(r, "x2", 0), y2 = Args.Int(r, "y2", 0);
|
|
231
|
+
uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
|
|
232
|
+
Overlay.Dodge(x1, y1); Overlay.Dodge(x2, y2);
|
|
233
|
+
Move(x1, y1);
|
|
234
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(x1, y1, "拖动", 1200);
|
|
235
|
+
Thread.Sleep(40);
|
|
236
|
+
Send(new List<Native.INPUT> { Mouse(down, 0) });
|
|
237
|
+
try
|
|
238
|
+
{
|
|
239
|
+
Thread.Sleep(60);
|
|
240
|
+
int steps = Math.Max(8, Math.Min(40, (int)(Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) / 20)));
|
|
241
|
+
for (int i = 1; i <= steps; i++)
|
|
242
|
+
{
|
|
243
|
+
Move(x1 + (x2 - x1) * i / steps, y1 + (y2 - y1) * i / steps);
|
|
244
|
+
Thread.Sleep(12);
|
|
245
|
+
}
|
|
246
|
+
Thread.Sleep(60);
|
|
247
|
+
}
|
|
248
|
+
finally { Send(new List<Native.INPUT> { Mouse(up, 0) }); }
|
|
249
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(x2, y2, "已拖动", 1200);
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
public static object Scroll(Dictionary<string, object> r)
|
|
254
|
+
{
|
|
255
|
+
int sx = 0, sy = 0;
|
|
256
|
+
bool hasPos = Args.Has(r, "x");
|
|
257
|
+
if (hasPos) { sx = Args.Int(r, "x", 0); sy = Args.Int(r, "y", 0); Overlay.Dodge(sx, sy); Move(sx, sy); Thread.Sleep(30); }
|
|
258
|
+
else { Native.POINT p; if (Native.GetCursorPos(out p)) { sx = p.X; sy = p.Y; } }
|
|
259
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(sx, sy, "滚动", 900);
|
|
260
|
+
int dx = Args.Int(r, "dx", 0), dy = Args.Int(r, "dy", 0);
|
|
261
|
+
var mods = Args.Ints(r, "modifiers");
|
|
262
|
+
Modifiers(mods, false);
|
|
263
|
+
try
|
|
264
|
+
{
|
|
265
|
+
// One notch per event, spaced out, so smooth-scrolling apps keep up.
|
|
266
|
+
for (int i = 0; i < Math.Abs(dy); i++) { Send(new List<Native.INPUT> { Mouse(Native.MOUSEEVENTF_WHEEL, (uint)(dy > 0 ? -120 : 120)) }); Thread.Sleep(15); }
|
|
267
|
+
for (int i = 0; i < Math.Abs(dx); i++) { Send(new List<Native.INPUT> { Mouse(Native.MOUSEEVENTF_HWHEEL, (uint)(dx > 0 ? 120 : -120)) }); Thread.Sleep(15); }
|
|
268
|
+
}
|
|
269
|
+
finally { Modifiers(mods, true); }
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// combos: [[vk, vk, ...], ...] — each combo presses in order and releases in reverse.
|
|
274
|
+
public static object Keys(Dictionary<string, object> r)
|
|
275
|
+
{
|
|
276
|
+
var combos = Args.IntLists(r, "combos");
|
|
277
|
+
int hold = Args.Int(r, "holdMs", 0);
|
|
278
|
+
foreach (var combo in combos)
|
|
279
|
+
{
|
|
280
|
+
var downs = new List<Native.INPUT>();
|
|
281
|
+
foreach (var vk in combo) downs.Add(Key(vk, false));
|
|
282
|
+
Send(downs);
|
|
283
|
+
Thread.Sleep(hold > 0 ? hold : 25);
|
|
284
|
+
var ups = new List<Native.INPUT>();
|
|
285
|
+
for (int i = combo.Count - 1; i >= 0; i--) ups.Add(Key(combo[i], true));
|
|
286
|
+
Send(ups);
|
|
287
|
+
Thread.Sleep(30);
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public static object KeyState(Dictionary<string, object> r)
|
|
293
|
+
{
|
|
294
|
+
var vks = Args.Ints(r, "vks");
|
|
295
|
+
Modifiers(vks, Args.Bool(r, "up", false));
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** charDelay > 0: one character at a time, like a person typing; otherwise fast batches. */
|
|
300
|
+
public static object Type(string text, int chunkDelay, int charDelay)
|
|
301
|
+
{
|
|
302
|
+
var batch = new List<Native.INPUT>();
|
|
303
|
+
int pause = charDelay > 0 ? charDelay : chunkDelay;
|
|
304
|
+
Action flush = delegate { if (batch.Count == 0) return; Send(batch); batch.Clear(); if (pause > 0) Thread.Sleep(pause); };
|
|
305
|
+
for (int i = 0; i < text.Length; i++)
|
|
306
|
+
{
|
|
307
|
+
char c = text[i];
|
|
308
|
+
if (c == '\r') { if (i + 1 < text.Length && text[i + 1] == '\n') continue; c = '\n'; }
|
|
309
|
+
if (c == '\n') { flush(); batch.Add(Key(0x0D, false)); batch.Add(Key(0x0D, true)); flush(); continue; }
|
|
310
|
+
if (c == '\t') { batch.Add(Key(0x09, false)); batch.Add(Key(0x09, true)); continue; }
|
|
311
|
+
batch.Add(Unicode(c, false)); batch.Add(Unicode(c, true));
|
|
312
|
+
if (char.IsHighSurrogate(c)) continue;
|
|
313
|
+
if (charDelay > 0 || batch.Count >= 40) flush();
|
|
314
|
+
}
|
|
315
|
+
flush();
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
public static object Paste(string text, bool restore, int waitMs)
|
|
320
|
+
{
|
|
321
|
+
// Keep every format the user had (images, files, rich text), not just text.
|
|
322
|
+
DataObject previous = null;
|
|
323
|
+
if (restore) { try { previous = Clip.Snapshot(); } catch (Exception) { } }
|
|
324
|
+
Clip.Set(text, true);
|
|
325
|
+
Thread.Sleep(40);
|
|
326
|
+
Send(new List<Native.INPUT> { Key(0x11, false), Key(0x56, false) });
|
|
327
|
+
Thread.Sleep(30);
|
|
328
|
+
Send(new List<Native.INPUT> { Key(0x56, true), Key(0x11, true) });
|
|
329
|
+
Thread.Sleep(Math.Max(60, waitMs)); // the app reads the clipboard asynchronously
|
|
330
|
+
if (restore) { try { Clip.Put(previous); } catch (Exception) { } }
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
internal static class Clip
|
|
336
|
+
{
|
|
337
|
+
public static string Get()
|
|
338
|
+
{
|
|
339
|
+
for (int i = 0; i < 5; i++)
|
|
340
|
+
{
|
|
341
|
+
try { return Clipboard.ContainsText() ? Clipboard.GetText() : ""; }
|
|
342
|
+
catch (ExternalException) { Thread.Sleep(50); }
|
|
343
|
+
}
|
|
344
|
+
throw new Exception("clipboard is busy");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** The user's clipboard before the agent first wrote to it, restored when control ends. */
|
|
348
|
+
static DataObject saved;
|
|
349
|
+
static bool hasSaved;
|
|
350
|
+
/** What the agent last put there, to tell whether the user copied something since. */
|
|
351
|
+
static string agentText;
|
|
352
|
+
|
|
353
|
+
static string Normalize(string text)
|
|
354
|
+
{
|
|
355
|
+
return string.IsNullOrEmpty(text) ? "" : text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Mark data as transient: clipboard history (Win+V), cloud clipboard and clipboard managers skip it. */
|
|
359
|
+
static void MarkPrivate(DataObject d)
|
|
360
|
+
{
|
|
361
|
+
d.SetData("ExcludeClipboardContentFromMonitorProcessing", false, new MemoryStream(new byte[] { 1, 0, 0, 0 }));
|
|
362
|
+
d.SetData("CanIncludeInClipboardHistory", false, new MemoryStream(BitConverter.GetBytes(0)));
|
|
363
|
+
d.SetData("CanUploadToCloudClipboard", false, new MemoryStream(BitConverter.GetBytes(0)));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Copy every format currently on the clipboard (null when empty). */
|
|
367
|
+
public static DataObject Snapshot()
|
|
368
|
+
{
|
|
369
|
+
for (int i = 0; i < 5; i++)
|
|
370
|
+
{
|
|
371
|
+
try
|
|
372
|
+
{
|
|
373
|
+
var src = Clipboard.GetDataObject();
|
|
374
|
+
if (src == null) return null;
|
|
375
|
+
var formats = src.GetFormats(false);
|
|
376
|
+
if (formats.Length == 0) return null;
|
|
377
|
+
var dst = new DataObject();
|
|
378
|
+
foreach (var f in formats)
|
|
379
|
+
{
|
|
380
|
+
if (f == "CanIncludeInClipboardHistory" || f == "CanUploadToCloudClipboard" || f == "ExcludeClipboardContentFromMonitorProcessing") continue;
|
|
381
|
+
try { var v = src.GetData(f, false); if (v != null) dst.SetData(f, false, v); } catch (Exception) { }
|
|
382
|
+
}
|
|
383
|
+
return dst;
|
|
384
|
+
}
|
|
385
|
+
catch (ExternalException) { Thread.Sleep(50); }
|
|
386
|
+
}
|
|
387
|
+
throw new Exception("clipboard is busy");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Put a snapshot back without adding a history entry (null clears). */
|
|
391
|
+
public static void Put(DataObject data)
|
|
392
|
+
{
|
|
393
|
+
for (int i = 0; i < 5; i++)
|
|
394
|
+
{
|
|
395
|
+
try
|
|
396
|
+
{
|
|
397
|
+
if (data == null) Clipboard.Clear();
|
|
398
|
+
else { MarkPrivate(data); Clipboard.SetDataObject(data, true); }
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
catch (ExternalException) { Thread.Sleep(50); }
|
|
402
|
+
}
|
|
403
|
+
throw new Exception("clipboard is busy");
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** The user's clipboard, kept aside while a streamed paste runs. */
|
|
407
|
+
static DataObject held;
|
|
408
|
+
static bool holding;
|
|
409
|
+
|
|
410
|
+
public static void Hold()
|
|
411
|
+
{
|
|
412
|
+
if (holding) return;
|
|
413
|
+
held = Snapshot();
|
|
414
|
+
holding = true;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
public static void Release()
|
|
418
|
+
{
|
|
419
|
+
if (!holding) return;
|
|
420
|
+
var data = held;
|
|
421
|
+
held = null; holding = false;
|
|
422
|
+
Put(data);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
public static void Set(string text, bool isPrivate)
|
|
426
|
+
{
|
|
427
|
+
text = Normalize(text);
|
|
428
|
+
for (int i = 0; i < 5; i++)
|
|
429
|
+
{
|
|
430
|
+
try
|
|
431
|
+
{
|
|
432
|
+
if (text.Length == 0) Clipboard.Clear();
|
|
433
|
+
else
|
|
434
|
+
{
|
|
435
|
+
var d = new DataObject();
|
|
436
|
+
d.SetData(DataFormats.UnicodeText, false, text);
|
|
437
|
+
if (isPrivate) MarkPrivate(d);
|
|
438
|
+
Clipboard.SetDataObject(d, true);
|
|
439
|
+
}
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
catch (ExternalException) { Thread.Sleep(50); }
|
|
443
|
+
}
|
|
444
|
+
throw new Exception("clipboard is busy");
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** The agent writes the clipboard: remember the user's content first (once per control session). */
|
|
448
|
+
public static void AgentSet(string text)
|
|
449
|
+
{
|
|
450
|
+
if (!hasSaved) { saved = Snapshot(); hasSaved = true; }
|
|
451
|
+
Set(text, true);
|
|
452
|
+
agentText = Normalize(text);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** Control ended: put the user's clipboard back, unless they copied something since the agent's last write. */
|
|
456
|
+
public static bool RestoreAgent()
|
|
457
|
+
{
|
|
458
|
+
if (!hasSaved) return false;
|
|
459
|
+
bool ours = false;
|
|
460
|
+
try { ours = Get() == agentText; } catch (Exception) { }
|
|
461
|
+
var data = saved;
|
|
462
|
+
saved = null; hasSaved = false; agentText = null;
|
|
463
|
+
if (!ours) return false;
|
|
464
|
+
Put(data);
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
/** While the agent moves, clicks or scrolls, the card lets the mouse through. */
|
|
471
|
+
internal sealed class PassThrough : IDisposable
|
|
472
|
+
{
|
|
473
|
+
readonly IntPtr[] hwnds;
|
|
474
|
+
readonly int[] oldEx;
|
|
475
|
+
bool active;
|
|
476
|
+
|
|
477
|
+
/** The DSH card and our progress card let injected pointer input through. */
|
|
478
|
+
public PassThrough()
|
|
479
|
+
{
|
|
480
|
+
hwnds = new[] { CardLayer.Hwnd, Overlay.PetHwnd };
|
|
481
|
+
oldEx = new int[hwnds.Length];
|
|
482
|
+
for (int i = 0; i < hwnds.Length; i++)
|
|
483
|
+
{
|
|
484
|
+
if (hwnds[i] == IntPtr.Zero || !Native.IsWindow(hwnds[i])) { hwnds[i] = IntPtr.Zero; continue; }
|
|
485
|
+
oldEx[i] = Native.GetWindowLong(hwnds[i], -20);
|
|
486
|
+
Native.SetWindowLong(hwnds[i], -20, oldEx[i] | 0x20 | 0x80000); // WS_EX_TRANSPARENT (layered already)
|
|
487
|
+
active = true;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
public void Dispose()
|
|
492
|
+
{
|
|
493
|
+
if (!active) return;
|
|
494
|
+
Thread.Sleep(60); // let the injected input reach its target first
|
|
495
|
+
for (int i = 0; i < hwnds.Length; i++)
|
|
496
|
+
if (hwnds[i] != IntPtr.Zero && Native.IsWindow(hwnds[i])) Native.SetWindowLong(hwnds[i], -20, oldEx[i]);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
}
|
package/helper/Native.cs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Win32 P/Invoke declarations and native structs (C# 5, in-box .NET 4 csc.exe).
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections;
|
|
5
|
+
using System.Collections.Generic;
|
|
6
|
+
using System.Diagnostics;
|
|
7
|
+
using System.Drawing;
|
|
8
|
+
using System.Drawing.Drawing2D;
|
|
9
|
+
using System.Drawing.Imaging;
|
|
10
|
+
using System.Drawing.Text;
|
|
11
|
+
using System.IO;
|
|
12
|
+
using System.Reflection;
|
|
13
|
+
using System.Runtime.InteropServices;
|
|
14
|
+
using System.Text;
|
|
15
|
+
using System.Threading;
|
|
16
|
+
using System.Web.Script.Serialization;
|
|
17
|
+
using System.Windows.Automation;
|
|
18
|
+
using System.Windows.Forms;
|
|
19
|
+
using Microsoft.Win32;
|
|
20
|
+
|
|
21
|
+
namespace DshComputerUse
|
|
22
|
+
{
|
|
23
|
+
internal static class Native
|
|
24
|
+
{
|
|
25
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr value);
|
|
26
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
27
|
+
|
|
28
|
+
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { X = x; Y = y; } }
|
|
29
|
+
[StructLayout(LayoutKind.Sequential)] public struct SIZE { public int cx; public int cy; public SIZE(int w, int h) { cx = w; cy = h; } }
|
|
30
|
+
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
|
|
31
|
+
|
|
32
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
33
|
+
public struct MOUSEINPUT { public int dx; public int dy; public uint mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
34
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
35
|
+
public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; }
|
|
36
|
+
[StructLayout(LayoutKind.Explicit)]
|
|
37
|
+
public struct InputUnion { [FieldOffset(0)] public MOUSEINPUT mi; [FieldOffset(0)] public KEYBDINPUT ki; }
|
|
38
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
39
|
+
public struct INPUT { public uint type; public InputUnion u; }
|
|
40
|
+
|
|
41
|
+
[DllImport("user32.dll", SetLastError = true)] public static extern uint SendInput(uint n, INPUT[] inputs, int size);
|
|
42
|
+
[DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
|
|
43
|
+
[DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT p);
|
|
44
|
+
[DllImport("user32.dll")] public static extern uint MapVirtualKey(uint code, uint mapType);
|
|
45
|
+
|
|
46
|
+
public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdc, ref RECT rect, IntPtr data);
|
|
47
|
+
[DllImport("user32.dll")] public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr clip, MonitorEnumProc proc, IntPtr data);
|
|
48
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
49
|
+
public struct MONITORINFOEX { public int cbSize; public RECT rcMonitor; public RECT rcWork; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDevice; }
|
|
50
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX info);
|
|
51
|
+
[DllImport("shcore.dll")] public static extern int GetDpiForMonitor(IntPtr hMonitor, int type, out uint dpiX, out uint dpiY);
|
|
52
|
+
|
|
53
|
+
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
|
|
54
|
+
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
|
|
55
|
+
[DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr dst, int x, int y, int w, int h, IntPtr src, int sx, int sy, uint rop);
|
|
56
|
+
[DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
|
|
57
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hdc);
|
|
58
|
+
[DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
|
|
59
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr obj);
|
|
60
|
+
|
|
61
|
+
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr data);
|
|
62
|
+
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc proc, IntPtr data);
|
|
63
|
+
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hwnd);
|
|
64
|
+
[DllImport("user32.dll")] public static extern bool IsWindow(IntPtr hwnd);
|
|
65
|
+
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hwnd);
|
|
66
|
+
[DllImport("user32.dll")] public static extern bool IsZoomed(IntPtr hwnd);
|
|
67
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr hwnd, StringBuilder sb, int max);
|
|
68
|
+
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hwnd);
|
|
69
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetClassName(IntPtr hwnd, StringBuilder sb, int max);
|
|
70
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr after, string cls, string title);
|
|
71
|
+
[DllImport("user32.dll")] public static extern IntPtr SendMessageTimeout(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam, uint flags, uint timeout, out IntPtr result);
|
|
72
|
+
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hwnd, uint cmd);
|
|
73
|
+
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hwnd, int index);
|
|
74
|
+
[DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hwnd, int index, int value);
|
|
75
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetProp(IntPtr hwnd, string name, IntPtr value);
|
|
76
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr GetProp(IntPtr hwnd, string name);
|
|
77
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr RemoveProp(IntPtr hwnd, string name);
|
|
78
|
+
[DllImport("user32.dll")] public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint key, out byte alpha, out uint flags);
|
|
79
|
+
[DllImport("user32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint key, byte alpha, uint flags);
|
|
80
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint pid);
|
|
81
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
82
|
+
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hwnd);
|
|
83
|
+
[DllImport("user32.dll")] public static extern bool BringWindowToTop(IntPtr hwnd);
|
|
84
|
+
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hwnd, int cmd);
|
|
85
|
+
[DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hwnd, bool altTab);
|
|
86
|
+
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint a, uint b, bool attach);
|
|
87
|
+
[DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();
|
|
88
|
+
[DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hwnd, uint msg, IntPtr w, IntPtr l);
|
|
89
|
+
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(POINT p);
|
|
90
|
+
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hwnd, uint flags);
|
|
91
|
+
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hwnd, IntPtr after, int x, int y, int w, int h, uint flags);
|
|
92
|
+
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hwnd, out RECT r);
|
|
93
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
94
|
+
public struct WINDOWPLACEMENT { public int length; public int flags; public int showCmd; public POINT ptMinPosition; public POINT ptMaxPosition; public RECT rcNormalPosition; }
|
|
95
|
+
[DllImport("user32.dll")] public static extern bool GetWindowPlacement(IntPtr hwnd, ref WINDOWPLACEMENT wp);
|
|
96
|
+
[DllImport("user32.dll")] public static extern bool SetWindowPlacement(IntPtr hwnd, ref WINDOWPLACEMENT wp);
|
|
97
|
+
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attr, out RECT value, int size);
|
|
98
|
+
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attr, out int value, int size);
|
|
99
|
+
|
|
100
|
+
[DllImport("kernel32.dll")] public static extern IntPtr OpenProcess(uint access, bool inherit, uint pid);
|
|
101
|
+
[DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr h);
|
|
102
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern bool QueryFullProcessImageName(IntPtr h, int flags, StringBuilder sb, ref int size);
|
|
103
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr GetModuleHandle(string name);
|
|
104
|
+
|
|
105
|
+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
106
|
+
public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; }
|
|
107
|
+
[DllImport("user32.dll", SetLastError = true)]
|
|
108
|
+
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, int flags);
|
|
109
|
+
[DllImport("user32.dll", SetLastError = true)]
|
|
110
|
+
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pptSrc, int crKey, ref BLENDFUNCTION pblend, int flags);
|
|
111
|
+
[DllImport("user32.dll")] public static extern bool SetWindowDisplayAffinity(IntPtr hwnd, uint affinity);
|
|
112
|
+
[DllImport("user32.dll")] public static extern bool ReleaseCapture();
|
|
113
|
+
[DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr w, IntPtr l);
|
|
114
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
115
|
+
public struct GUITHREADINFO { public int cbSize; public int flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rcCaret; }
|
|
116
|
+
[DllImport("user32.dll")] public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
|
|
117
|
+
|
|
118
|
+
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
119
|
+
public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
120
|
+
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc fn, IntPtr hMod, uint threadId);
|
|
121
|
+
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc fn, IntPtr hMod, uint threadId);
|
|
122
|
+
[DllImport("user32.dll")] public static extern bool UnhookWindowsHookEx(IntPtr hook);
|
|
123
|
+
[DllImport("user32.dll")] public static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wParam, IntPtr lParam);
|
|
124
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
125
|
+
public struct KBDLLHOOKSTRUCT { public uint vkCode; public uint scanCode; public uint flags; public uint time; public IntPtr dwExtraInfo; }
|
|
126
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
127
|
+
public struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; }
|
|
128
|
+
|
|
129
|
+
public const uint INPUT_MOUSE = 0, INPUT_KEYBOARD = 1;
|
|
130
|
+
public const uint KEYEVENTF_EXTENDEDKEY = 0x1, KEYEVENTF_KEYUP = 0x2, KEYEVENTF_UNICODE = 0x4;
|
|
131
|
+
public const uint MOUSEEVENTF_MOVE = 0x1, MOUSEEVENTF_LEFTDOWN = 0x2, MOUSEEVENTF_LEFTUP = 0x4, MOUSEEVENTF_RIGHTDOWN = 0x8, MOUSEEVENTF_RIGHTUP = 0x10,
|
|
132
|
+
MOUSEEVENTF_MIDDLEDOWN = 0x20, MOUSEEVENTF_MIDDLEUP = 0x40, MOUSEEVENTF_WHEEL = 0x800, MOUSEEVENTF_HWHEEL = 0x1000;
|
|
133
|
+
public static readonly IntPtr Marker = new IntPtr(0x44534843); // "DSHC"
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
}
|