2

I using SendInput to simulate mouse input and so far I can successfully simulate left, right and middle clicks. However, I cannot seem to simulate the xButtons properly. I am defining an input structure as follows:

Win32.INPUT i = new Win32.INPUT();
            i.type = Win32.INPUTF.MOUSE;
            i.mi.dx = 0;
            i.mi.dy = 0;
            i.mi.mouseData = 0;
            i.mi.time = 0;
            i.mi.dwExtraInfo = UIntPtr.Zero;
            i.mi.dwFlags = MouseButton(e.Button, true);
            Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT()));

MouseButton is defined as follows, and I believe this is where the problem is.

private Win32.MOUSEEVENTF MouseButton(MouseButtons mb, bool up)
    {
        switch (mb)
        {
            case MouseButtons.Left:
                return up ? Win32.MOUSEEVENTF.LEFTUP : Win32.MOUSEEVENTF.LEFTDOWN;
            case MouseButtons.Right:
                return up ? Win32.MOUSEEVENTF.RIGHTUP : Win32.MOUSEEVENTF.RIGHTDOWN;
            case MouseButtons.Middle:
                return up ? Win32.MOUSEEVENTF.MIDDLEUP : Win32.MOUSEEVENTF.MIDDLEDOWN;
            case MouseButtons.XButton1:
                return up ?  Win32.MOUSEEVENTF.XBUTTON1|Win32.MOUSEEVENTF.XUP  : Win32.MOUSEEVENTF.XBUTTON1 | Win32.MOUSEEVENTF.XDOWN;
            case MouseButtons.XButton2:
                return up ? Win32.MOUSEEVENTF.XBUTTON2 | Win32.MOUSEEVENTF.XUP : Win32.MOUSEEVENTF.XBUTTON2 | Win32.MOUSEEVENTF.XDOWN;
            default:
                return 0u;
        }
    }
Alex
  • 69
  • 5
  • Did you use this link to build your Win32 class? http://www.pinvoke.net/default.aspx/user32/SendInput.html – digEmAll Jan 07 '11 at 11:00
  • I did not build the Win32 class I am using, but the one I am using is not identical to the one found at pinvoke.net/default.aspx/user32/SendInput.html, though it does have the same behavior. – Alex Jan 18 '11 at 04:22

1 Answers1

4

It turns out I was trying to specify which button was pressed in the wrong place. i.mi.dwFlags should only specify either Win32.MOUSEEVENTF.XDOWNor Win32.MOUSEEVENTF.XUP. The button pressed should be set using i.mi.mouseData.

So to simulate x button input I would do the following:

Win32.INPUT i = new Win32.INPUT();
            i.type = Win32.INPUTF.MOUSE;
            i.mi.dx = 0;
            i.mi.dy = 0;
            switch (e.Button)
            {
                case System.Windows.Forms.MouseButtons.XButton1:
                    i.mi.mouseData = (uint)Win32.MOUSEEVENTF.XBUTTON1;
                    break;
                case System.Windows.Forms.MouseButtons.XButton2:
                    i.mi.mouseData = (uint)Win32.MOUSEEVENTF.XBUTTON2;
                    break;
                default:
                    i.mi.mouseData = 0;
                    break;
            }

            i.mi.time = 0;
            i.mi.dwExtraInfo = UIntPtr.Zero;
            i.mi.dwFlags = Win32.MOUSEEVENTF.XUP;
            Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT()));
Alex
  • 69
  • 5
  • 1
    Alex, thanks for posting your question / answer! This is really helping me right now. Wish more people would answer their own questions when they found a solution! – Lunivore Jul 03 '11 at 10:51