forked from yangzhongke/NetAutoGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsKeyboardController.cs
More file actions
96 lines (86 loc) · 3.16 KB
/
Copy pathWindowsKeyboardController.cs
File metadata and controls
96 lines (86 loc) · 3.16 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
using NetAutoGUI.Internals;
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using Vanara.PInvoke;
namespace NetAutoGUI.Windows
{
[SupportedOSPlatform("windows")]
internal class WindowsKeyboardController : IKeyboardController
{
public void KeyDown(VirtualKeyCode key)
{
var inputBuilder = new InputBuilder().AddKeyDown(key);
SendInputs(inputBuilder);
}
public void KeyUp(VirtualKeyCode key)
{
var inputBuilder = new InputBuilder().AddKeyUp(key);
SendInputs(inputBuilder);
}
public void Press(params VirtualKeyCode[] keys)
{
var inputBuilder = new InputBuilder();
foreach (var key in keys)
{
inputBuilder.AddKeyPress(key);
}
SendInputs(inputBuilder);
}
public void Write(char c)
{
var inputBuilder = new InputBuilder().AddCharacter(c);
SendInputs(inputBuilder);
}
public void Write(string s)
{
var inputBuilder = new InputBuilder().AddCharacters(s);
SendInputs(inputBuilder);
}
public void Write(string s, double intervalInSeconds)
{
ValidationHelpers.NotNegative(intervalInSeconds, nameof(intervalInSeconds));
foreach (char c in s)
{
Write(c);
Thread.Sleep((int)(intervalInSeconds * 1000));
}
}
public async Task WriteAsync(string s, double intervalInSeconds, CancellationToken cancellationToken = default)
{
ValidationHelpers.NotNegative(intervalInSeconds, nameof(intervalInSeconds));
foreach (char c in s)
{
Write(c);
await Task.Delay((int)(intervalInSeconds * 1000), cancellationToken);
}
}
public IDisposable Hold(VirtualKeyCode key)
{
return new KeyHoldContext(key, this);
}
public void HotKey(params VirtualKeyCode[] keys)
{
foreach (var key in keys)
{
KeyDown(key);
}
for (int i = keys.Length - 1; i >= 0; i--)
{
var key = keys[i];
KeyUp(key);
}
}
private void SendInputs(InputBuilder inputBuilder)
{
var inputs = inputBuilder.ToArray();
var ret = User32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(User32.INPUT)));
if (ret != inputs.Length)
{
throw new Exception("Some simulated input commands were not sent successfully. The most common reason for this happening are the security features of Windows including User Interface Privacy Isolation (UIPI). Your application can only send commands to applications of the same or lower elevation. Similarly certain commands are restricted to Accessibility/UIAutomation applications. Refer to the project home page and the code samples for more information.");
}
}
}
}