Repository navigation
Expand file tree
/
Copy pathKeyboardSimulator.cs
More file actions
87 lines (76 loc) · 2.57 KB
/
Copy pathKeyboardSimulator.cs
File metadata and controls
87 lines (76 loc) · 2.57 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
using System;
using System.Runtime.InteropServices;
namespace WhisperTyper
{
public static class KeyboardSimulator
{
private const int INPUT_KEYBOARD = 1;
private const uint KEYEVENTF_UNICODE = 0x0004;
private const uint KEYEVENTF_KEYUP = 0x0002;
[StructLayout(LayoutKind.Explicit, Size = 40)]
private struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(8)]
public KEYBDINPUT ki;
}
[StructLayout(LayoutKind.Sequential)]
private struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
public static void SimulateTypeString(string text)
{
if (string.IsNullOrEmpty(text)) return;
INPUT[] inputs = new INPUT[text.Length * 2];
int cbSize = Marshal.SizeOf(typeof(INPUT));
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
// Key Down
inputs[i * 2] = new INPUT
{
type = INPUT_KEYBOARD,
ki = new KEYBDINPUT
{
wVk = 0,
wScan = c,
dwFlags = KEYEVENTF_UNICODE,
time = 0,
dwExtraInfo = IntPtr.Zero
}
};
// Key Up
inputs[i * 2 + 1] = new INPUT
{
type = INPUT_KEYBOARD,
ki = new KEYBDINPUT
{
wVk = 0,
wScan = c,
dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP,
time = 0,
dwExtraInfo = IntPtr.Zero
}
};
}
uint result = SendInput((uint)inputs.Length, inputs, cbSize);
if (result == 0)
{
int error = Marshal.GetLastWin32Error();
System.Diagnostics.Debug.WriteLine($"SendInput failed with Win32 error code: {error}");
}
else
{
System.Diagnostics.Debug.WriteLine($"SendInput successfully sent {result} keyboard events.");
}
}
}
}