-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindows.cs
77 lines (70 loc) · 2.36 KB
/
Windows.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Capybara
{
/// <summary>
/// Contains utility functions provided by the operating system
/// </summary>
abstract class Windows
{
//Required extern functions
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
protected static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
//MOUSEINPUT dwFlags:
public const uint LEFT_DOWN = 0x02,
LEFT_UP = 0x04,
RIGHT_DOWN = 0x08,
RIGHT_UP = 0x10;
[StructLayout(LayoutKind.Sequential)]
protected struct INPUT
{
public uint type; //mouse = 0, keyboard = 1, hardware = 2
public MOUSEINPUT mi;
//Keyboard and hardware emulation is not required by Capybara
//MOUSEINPUT is also larger than KEYBDINPUT and HARDWAREINPUT so INPUT remains the correct size
public INPUT(uint dwFlags)
: this(dwFlags, 0, 0)
{ }
public INPUT(uint dwFlags, int dx, int dy)
{
this.type = 0;
this.mi = new MOUSEINPUT
{
dwFlags = dwFlags,
dx = dx,
dy = dy,
};
}
}
[StructLayout(LayoutKind.Sequential)]
protected struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
public static void SendClick(uint flag, int dx = 0, int dy = 0)
{
if (flag != 0)
{
if (SendInput(1, new INPUT[] { new INPUT(flag, dx, dy) }, Marshal.SizeOf(typeof(INPUT))) == 0)
{
#if DEBUG
System.Diagnostics.Debug.Fail("SendInput returned zero");
#endif
}
}
}
}
}