S
Sin Jeong-hun
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}