0

I have two machines: A and B

On A, I get the current (local) mouse position (x & y) and send send that mouse position over my local network to machine B. Machine B takes the incoming position X and Y and simulate the mouse movement using the example found here. It all works fine and dandy - I can see the mouse moving, but for some reason, it does not affect the Window in the foreground on machine B.

What is this "Window"? It is a Unity3D application - a game. I expect that the mouse movement would cause the in-game camera to move around. Interestingly, if I do as described and then stop moving the mouse on machine A... and then move the mouse via the touchpad (or regular mouse) on machine B, it moves the in-game camera, as expected!

What is going on?

Community
  • 1
  • 1
pookie
  • 3,796
  • 6
  • 49
  • 105
  • I think you need to look at using CBT hooks on machine "B" to get what you want. See [here](http://stackoverflow.com/questions/4267881/cbt-hooks-in-windows-what-does-cbt-stand-for) – GreatAndPowerfulOz Jul 29 '16 at 19:59

1 Answers1

0

It seems that, for whatever reason, the code taken from here

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

does not work...properly! My mouse would move, as expected, on the remote machine but the application that I was bringing to the foreground was not detecting the mouse movements.

However, I did manage to get this to work using mouse_event:

[Flags]
public enum MouseEventFlags
{
    LeftDown = 0x00000002,
    LeftUp = 0x00000004,
    MiddleDown = 0x00000020,
    MiddleUp = 0x00000040,
    Move = 0x00000001,
    Absolute = 0x00008000,
    RightDown = 0x00000008,
    RightUp = 0x00000010
}

[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

public static void MouseEvent(MouseEventFlags value, Point position)
{
    MousePoint position = position;

    mouse_event
        ((int)value,
         position.X,
         position.Y,
         0,
         0)
        ;
}

[StructLayout(LayoutKind.Sequential)]
public struct MousePoint
{
    public int X;
    public int Y;

    public MousePoint(int x, int y)
    {
        X = x;
        Y = y;
    }

}

To use it, just call:

mouse_event(MouseEventFlags.Move,new MousePoint{X = YOUR_X, Y = YOUR_Y});

All credit goes to this SO answer.

Community
  • 1
  • 1
pookie
  • 3,796
  • 6
  • 49
  • 105