SendInput MOUSEINPUT

monolith

New member
Joined
Feb 28, 2007
Messages
3
Hi there, Im a relative C# noob, trying to develop an app that will help me automate some of my everyday processes that are very basic and repetitive, but require me to pass variables between programs etc. Ive been scouring the MSDN pages and http://www.pinvoke.net amongst other places to find out about mouse and key automation, and i have 90% of what I need - how to call functions from Win32 dlls, marshal the data etc, the only thing Im in not sure of is how to actually execute it:

Found this very useful snippet at http://blogs.msdn.com/robgruen/archive/2004/05/10/129221.aspx

Code:
[DllImport("User32.dll", SetLastError=true)]

       public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize);

 

       public struct INPUT

       {

              public int type;

              public MOUSEINPUT mi;

       }

 

       public struct MOUSEINPUT

       {

              public int dx;

              public int dy;

              public int mouseData;

              public int dwFlags;

              public int time;

              public int dwExtraInfo;

       }

       // Call the API

       int resSendInput;

       resSendInput = SendInput(1, ref input, Marshal.SizeOf(input));

       if (resSendInput == 0 || Marshal.GetLastWin32Error() != 0)

              System.Diagnostics.Debug.WriteLine(Marshal.GetLastWin32Error());

Also http://msdn2.microsoft.com/en-us/library/ms646273.aspx
Goes into depth about the actual MOUSEINPUT structure, but I have no idea how i would actually structure a call within the Main() method. Its probably intuitive and Im being stupid.
 
This sends the mouse cursor to the bottom left of the screen and clicks the Start button. It works by sending an array of Input structs to the p/invoked SendInput function.

The Input structs are built out of INPUT and MOUSEINPUT structures.

Code:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;


public class MainProg
{
    static void Main()
    {
        Input[] input = new Input[2];

        for (int i = 0; i < input.Length; i++)
        {
            input[i].type = INPUT.MOUSE;
            input[i].mi.dx = 0;
            input[i].mi.dy = 65535;
            input[i].mi.dwFlags = MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE;

            if ((i % 2) == 0)
                input[i].mi.dwFlags |= MOUSEEVENTF.LEFTDOWN;
            else
                input[i].mi.dwFlags |= MOUSEEVENTF.LEFTUP;
        }

        UInt32 r = User32.SendInput((UInt32)input.Length, input, Marshal.SizeOf(input[0]));
        Debug.Assert(r > 0);  
    }
}

    internal class INPUT
    {
        public const int MOUSE = 0;
        public const int KEYBOARD = 1;
        public const int HARDWARE = 2;
    }

    internal class MOUSEEVENTF
    {
        public const int MOVE = 0x0001; /* mouse move */
        public const int LEFTDOWN = 0x0002; /* left button down */
        public const int LEFTUP = 0x0004; /* left button up */
        public const int RIGHTDOWN = 0x0008; /* right button down */
        public const int RIGHTUP = 0x0010; /* right button up */
        public const int MIDDLEDOWN = 0x0020; /* middle button down */
        public const int MIDDLEUP = 0x0040; /* middle button up */
        public const int XDOWN = 0x0080; /* x button down */
        public const int XUP = 0x0100; /* x button down */
        public const int WHEEL = 0x0800; /* wheel button rolled */
        public const int VIRTUALDESK = 0x4000; /* map to entire virtual desktop */
        public const int ABSOLUTE = 0x8000; /* absolute move */
    }


    [StructLayout(LayoutKind.Sequential)]
    internal struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct KEYBDINPUT
    {
        public short wVk;
        public short wScan;
        public int dwFlags;
        public int 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 KEYBDINPUT ki;
        [FieldOffset(4)]
        public HARDWAREINPUT hi;
    }

    internal class User32
    {
        private User32() { }

        [DllImport("User32.dll")]
        public static extern UInt32 SendInput
        (
            UInt32 nInputs,
            Input[] pInputs,
            Int32 cbSize
        );

    }


        


    }

This example relied heavily on this article - http://www.codeproject.com/csharp/freecelldiscombobulator.asp
 
Back
Top