For example, I found a sample online which works for Keyboard inputs. It is
here:
http://split-s.blogspot.com/2005/01/...n-windows.html
But I need to do both keyboard/mouse inputs. I found other samples (which
are either incomplete or dont work when tried directly) which do both
keyboard and mouse but they do something with making a kind of generic struct
called "INPUT" which has properties for both keyboard and mouse, so you
apparently set the values for the input type you want and then send that
INPUT struct to SendInput.
Here's what I copied/adapted:
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBOARDINPUT
{
public uint type;
public ushort vk;
public ushort scanCode;
public uint flags;
public uint time;
public uint extrainfo;
public uint padding1;
public uint padding2;
}
[ StructLayout( LayoutKind.Sequential ) ]
internal struct MOUSEINPUT
{
public uint dx;
public uint dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[ StructLayout( LayoutKind.Sequential ) ]
internal struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[ StructLayout( LayoutKind.Explicit ) ]
internal struct INPUT
{
[ FieldOffset( 0 ) ] public int type;
[ FieldOffset( 4 ) ] public MOUSEINPUT mi;
[ FieldOffset( 4 ) ] public KEYBOARDINPUT ki;
[ FieldOffset( 4 ) ] public HARDWAREINPUT hi;
}
[DllImport("User32.dll")]
private static extern uint SendInput(uint numberOfInputs, ref INPUT input,
int structSize);
private void sendKey(int scanCode, bool press)
{
INPUT input = new INPUT();
input.type = Win32Consts.INPUT_KEYBOARD;
input.ki = new KEYBOARDINPUT();
input.ki.flags = KEY_SCANCODE;
if ((scanCode & 0xFF00) == 0xE000)
{ // extended key?
input.ki.flags |= KEY_EXTENDED;
}
if (press)
{ // press?
input.ki.scanCode = (ushort) (scanCode & 0xFF);
}
else
{ // release?
input.ki.scanCode = (ushort)scanCode;
input.ki.flags |= KEY_UP;
}
uint result = SendInput(1, ref input, Marshal.SizeOf(input));
if (result != 1)
{
throw new Exception("Could not send key: " + scanCode);
}
}
When I changed the original example from the one in the link I posted above
to this code I pasted where it uses the INPUT struct, it keeps failing. I
really don't understand why... Any ideas?