Hello,
I am using SendInput() for the first time, having only heard of it about a week ago. I have written a test using SendInput() to hold down the CTRL key. After a great deal of pain, I got this to work when I run the test using the TestDriven.net tool, an addin for Microsoft Visual Studio:
However, when I run it as a unit test in VS 2008, (right-clicking on the test and choosing "Run Tests" from the menu, or choosing RunSelection from the TestView form), I get a return value of 0 from SendInput().
I'm at a complete loss, thanks for your help.
Here is my code:
[TestMethod]
public void SendInputTest()
{
Keyboard.NonCharKeyDown(VK.CONTROL);
}
public static void NonCharKeyDown(VK vk)
{
INPUT[] inputs = new INPUT[1];
inputs[0].type = WindowsAPI.INPUT_KEYBOARD;
inputs[0].ki.dwFlags = 0;
inputs[0].ki.wVk = (short)vk;
uint intReturn = WindowsAPI.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0]));
if (intReturn != 1)
{
throw new Exception("Could not send key: " + vk);
}
}
public enum VK : short
{
SHIFT = 0x10,
CONTROL = 0x11,
MENU = 0x12
}
[DllImport("user32.dll")]
public static extern IntPtr GetMessageExtraInfo();
public const int INPUT_MOUSE = 0;
public const int INPUT_KEYBOARD = 1; <---------------
public const int INPUT_HARDWARE = 2;
public const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
public const uint KEYEVENTF_KEYUP = 0x0002;
public const uint KEYEVENTF_UNICODE = 0x0004;
public const uint KEYEVENTF_SCANCODE = 0x0008;
public const uint XBUTTON1 = 0x0001;
public const uint XBUTTON2 = 0x0002;
public const uint MOUSEEVENTF_MOVE = 0x0001;
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
public const uint MOUSEEVENTF_XDOWN = 0x0080;
public const uint MOUSEEVENTF_XUP = 0x0100;
public const uint MOUSEEVENTF_WHEEL = 0x0800;
public const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
public const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public short wVk;
public short wScan;
public uint dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll")]
public static extern uint SendInput(uint numberOfInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] input, int structSize);
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(8)] //*
public MOUSEINPUT mi;
[FieldOffset(8)] //*
public KEYBDINPUT ki;
[FieldOffset(8)] //*
public HARDWAREINPUT hi;
}