-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeMethods.cs
90 lines (76 loc) · 2.65 KB
/
NativeMethods.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
78
79
80
81
82
83
84
85
86
87
88
89
90
/////////////////////////////////////////////////////////////////////////
//
// Copyright © Microsoft Corporation. All rights reserved.
// This code is a “supplement” under the terms of the
// Microsoft Kinect for Windows SDK (Beta) from Microsoft Research
// License Agreement: http://research.microsoft.com/KinectSDK-ToU
// and is licensed under the terms of that license agreement.
//
/////////////////////////////////////////////////////////////////////////
// get past MouseData not being initialized warning...it needs to be there for p/invoke
#pragma warning disable 0649
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Microsoft.Research.Kinect.Samples.CursorControl
{
internal struct MouseInput
{
public int X;
public int Y;
public uint MouseData;
public uint Flags;
public uint Time;
public IntPtr ExtraInfo;
}
internal struct Input
{
public int Type;
public MouseInput MouseInput;
}
public static class NativeMethods
{
public const int InputMouse = 0;
public const int MouseEventMove = 0x01;
public const int MouseEventLeftDown = 0x02;
public const int MouseEventLeftUp = 0x04;
public const int MouseEventRightDown = 0x08;
public const int MouseEventRightUp = 0x10;
public const int MouseEventAbsolute = 0x8000;
private static bool lastLeftDown;
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint numInputs, Input[] inputs, int size);
public static void SendMouseInput(int positionX, int positionY, int maxX, int maxY, bool leftDown)
{
if(positionX > int.MaxValue)
throw new ArgumentOutOfRangeException("positionX");
if(positionY > int.MaxValue)
throw new ArgumentOutOfRangeException("positionY");
Input[] i = new Input[2];
// move the mouse to the position specified
i[0] = new Input();
i[0].Type = InputMouse;
i[0].MouseInput.X = (positionX * 65535) / maxX;
i[0].MouseInput.Y = (positionY * 65535) / maxY;
i[0].MouseInput.Flags = MouseEventAbsolute | MouseEventMove;
// determine if we need to send a mouse down or mouse up event
if(!lastLeftDown && leftDown)
{
i[1] = new Input();
i[1].Type = InputMouse;
i[1].MouseInput.Flags = MouseEventLeftDown;
}
else if(lastLeftDown && !leftDown)
{
i[1] = new Input();
i[1].Type = InputMouse;
i[1].MouseInput.Flags = MouseEventLeftUp;
}
lastLeftDown = leftDown;
// send it off
uint result = SendInput(2, i, Marshal.SizeOf(i[0]));
if(result == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}