Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Win32_RawInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ internal static extern int HidP_GetUsages(//aka "GetButtonsCurrentlyPressedInThi
[In, Out] ref uint usageLength,
IntPtr pPreparsedData,
IntPtr refHidReportBuffer,
ulong reportLength
uint reportLength
);
}

Expand Down
111 changes: 108 additions & 3 deletions src/Win32_SendInput.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Interop wrapper for programmatically generating keystroke, and manipulating mousewheel.
* Not done: mouse-move, mouse buttons.
* Interop wrapper for programmatically generating keystrokes, and manipulating mouse.
*
* TODO: test with multi-monitor.. coords relative to "primary" desktop? virtual desktop-space?
*/
using System;
using System.ComponentModel;
Expand All @@ -27,6 +28,50 @@ public static void SendKeystroke( ushort scanCode, bool keyDown, bool isExtended
return;
}

//----------------------------------------
public static void MoveMouseAbsolute( int x, int y )
{
//?: multiple monitors? virtual desktop?
_Interop_User32.InputStruct[] inputs = new _Interop_User32.InputStruct[] {
_PrepMousePosition(x, y, virtualCoords:true ) //?
};

int status = _Interop_User32.SendInput(inputs.Length, inputs, _Interop_User32.InputStruct.MarshalSize);
if (status != inputs.Length)
throw new Win32Exception();

return;
}

//----------------------------------------
public static void MoveMouseRelative( int dx, int dy )
{
_Interop_User32.InputStruct[] inputs = new _Interop_User32.InputStruct[] {
_PrepMouseMovement(dx, dy, virtualCoords:true ) //?
};

int status = _Interop_User32.SendInput(inputs.Length, inputs, _Interop_User32.InputStruct.MarshalSize);
if (status != inputs.Length)
throw new Win32Exception();

return;
}

//----------------------------------------
public static void SendMouseButton( int mouseButton, bool buttonDown )
{
//?: swap mouse buttons?
_Interop_User32.InputStruct[] inputs = new _Interop_User32.InputStruct[] {
_PrepMouseButton(mouseButton, buttonDown)
};

int status = _Interop_User32.SendInput(inputs.Length, inputs, _Interop_User32.InputStruct.MarshalSize);
if (status != inputs.Length)
throw new Win32Exception();

return;
}

//----------------------------------------
public static void MoveMouseWheel( int wheelDelta )
{
Expand All @@ -44,6 +89,56 @@ public static void MoveMouseWheel( int wheelDelta )
//--------------------------------------------------------------
// Managed helpers

static _Interop_User32.InputStruct _PrepMousePosition( int x, int y, bool virtualCoords )
{
_Interop_User32.InputStruct inputStruct = new _Interop_User32.InputStruct();
inputStruct.type = _Interop_User32.INPUT_MOUSE;
inputStruct._union.mouseInput.dx = x;
inputStruct._union.mouseInput.dy = y;
inputStruct._union.mouseInput.flags = (_Interop_User32.MOUSEEVENTF_MOVE | _Interop_User32.MOUSEEVENTF_ABSOLUTE);

if (virtualCoords)
inputStruct._union.mouseInput.flags |= _Interop_User32.MOUSEEVENTF_VIRTUALDESK;

return inputStruct;
}

static _Interop_User32.InputStruct _PrepMouseMovement( int dx, int dy, bool virtualCoords )
{
_Interop_User32.InputStruct inputStruct = new _Interop_User32.InputStruct();
inputStruct.type = _Interop_User32.INPUT_MOUSE;
inputStruct._union.mouseInput.dx = dx;
inputStruct._union.mouseInput.dy = dy;
inputStruct._union.mouseInput.flags = (_Interop_User32.MOUSEEVENTF_MOVE);

if (virtualCoords)
inputStruct._union.mouseInput.flags |= _Interop_User32.MOUSEEVENTF_VIRTUALDESK;

return inputStruct;
}

static _Interop_User32.InputStruct _PrepMouseButton( int mouseButton, bool buttonDown )
{
_Interop_User32.InputStruct inputStruct = new _Interop_User32.InputStruct();
inputStruct.type = _Interop_User32.INPUT_MOUSE;
switch (mouseButton)
{
case 0: //left
inputStruct._union.mouseInput.flags = buttonDown ? _Interop_User32.MOUSEEVENTF_LEFTDOWN : _Interop_User32.MOUSEEVENTF_LEFTUP;
break;
case 1: //right
inputStruct._union.mouseInput.flags = buttonDown ? _Interop_User32.MOUSEEVENTF_RIGHTDOWN : _Interop_User32.MOUSEEVENTF_RIGHTUP;
break;
case 2: //middle
inputStruct._union.mouseInput.flags = buttonDown ? _Interop_User32.MOUSEEVENTF_MIDDLEDOWN : _Interop_User32.MOUSEEVENTF_MIDDLEUP;
break;
default:
throw new ArgumentException();
}

return inputStruct;
}

static _Interop_User32.InputStruct _PrepMouseWheelDelta( int wheelDelta )
{
_Interop_User32.InputStruct inputStruct = new _Interop_User32.InputStruct();
Expand Down Expand Up @@ -79,8 +174,18 @@ static class _Interop_User32
internal const uint INPUT_MOUSE = 0;
internal const uint INPUT_KEYBOARD = 1;

internal const ushort MOUSEEVENTF_MOVE = 0x0001;
internal const ushort MOUSEEVENTF_LEFTDOWN = 0x0002;
internal const ushort MOUSEEVENTF_LEFTUP = 0x0004;
internal const ushort MOUSEEVENTF_RIGHTDOWN = 0x0008;
internal const ushort MOUSEEVENTF_RIGHTUP = 0x0010;
internal const ushort MOUSEEVENTF_MIDDLEDOWN = 0x0020;
internal const ushort MOUSEEVENTF_MIDDLEUP = 0x0040;
internal const ushort MOUSEEVENTF_WHEEL = 0x0800;

internal const ushort MOUSEEVENTF_VIRTUALDESK = 0x4000;
internal const ushort MOUSEEVENTF_ABSOLUTE = 0x8000;

internal const ushort KEYEVENTF_SCANCODE = 0x0008;
internal const ushort KEYEVENTF_KEYUP = 0x0002;
internal const ushort KEYEVENTF_EXTENDEDKEY = 0x0001;
Expand Down Expand Up @@ -108,7 +213,7 @@ internal struct InputStruct_union
[FieldOffset(0)]
internal KeybdInputStruct keybdInput;

//NB: This option seems unimplemented? And smaller size than others, so irrelevant.
//NB: This option seems unimplemented? And smaller size than others, so irrelevant as part of the union.
//[FieldOffset(0)]
//internal HARDWAREINPUT hi;
}
Expand Down