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.Componen tModel;
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_ABS OLUTE | MOUSEEVENTF_MOV E;
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("user 32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(La youtKind.Explic it)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(La youtKind.Sequen tial)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(La youtKind.Sequen tial)]
struct KEYBDINPUT
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(La youtKind.Sequen tial)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOV E = 0x0001;
const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
const uint MOUSEEVENTF_LEF TUP = 0x0004;
const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
const uint MOUSEEVENTF_RIG HTUP = 0x0010;
const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
const uint MOUSEEVENTF_MID DLEUP = 0x0040;
const uint MOUSEEVENTF_XDO WN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHE EL = 0x0800;
const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
const uint MOUSEEVENTF_ABS OLUTE = 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.Componen tModel;
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_ABS OLUTE | MOUSEEVENTF_MOV E;
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("user 32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(La youtKind.Explic it)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
[StructLayout(La youtKind.Sequen tial)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(La youtKind.Sequen tial)]
struct KEYBDINPUT
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(La youtKind.Sequen tial)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOV E = 0x0001;
const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
const uint MOUSEEVENTF_LEF TUP = 0x0004;
const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
const uint MOUSEEVENTF_RIG HTUP = 0x0010;
const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
const uint MOUSEEVENTF_MID DLEUP = 0x0040;
const uint MOUSEEVENTF_XDO WN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHE EL = 0x0800;
const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}
Comment