如何将相对鼠标移动发送到另一台电脑

时间:2009-04-08 21:42:01

标签: c# controls mouse hook

我捕捉到Windows鼠标移动事件并计算相对鼠标移动以将其发送到另一台PC。到目前为止一切顺利,效果很好。

但是,如果我在发送鼠标坐标(客户端)的屏幕上阻止鼠标移动或到达屏幕的一侧,则会出现第二个鼠标事件,它会被鼠标点击,然后将鼠标按回来。 / p>

我的第一个想法是记录相对运动并忽略每个“倒置”运动。但我正在寻找一种更好的方法。

首先我打电话:

Cursor.Position = new Point(0, 0);

    void HookManager_MouseMoveExt(object sender, MouseEventExtArgs e)
    {
        Logger.Log(String.Format("Pos: {0} {1} Delta: {2} {3}", e.X, e.Y, e.DeltaX, e.DeltaY), LogLevel.Info);

        if (hasControl)
            server.MouseMove(e.DeltaX, e.DeltaY, true); // send the coordinates to the client

        e.Handled = true; // Don't move the mouse
    }

现在我启动应用程序并将鼠标移动到左上方向。我只想收到负增量,但这种情况会发生:

09.04.2009 00:29:31 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: 0 0 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:31 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:31 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: -1 0
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: 0 0 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: -1 -1
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -2 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 1 0 // Here it starts to snap back first time
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -2 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -2 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -2 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 -2 Delta: 1 0
09.04.2009 00:29:31 <10> Pos: -3 -1 Delta: 0 1
09.04.2009 00:29:31 <10> Pos: -2 -2 Delta: 1 0
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: 1 1
09.04.2009 00:29:31 <10> Pos: -3 -1 Delta: 0 0
09.04.2009 00:29:31 <10> Pos: -1 0 Delta: 2 0
09.04.2009 00:29:31 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:31 <10> Pos: -1 -1 Delta: -1 0
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:32 <10> Pos: 0 0 Delta: 0 0
09.04.2009 00:29:32 <10> Pos: -1 -1 Delta: -1 -1
09.04.2009 00:29:32 <10> Pos: -1 -1 Delta: 0 0
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:32 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:32 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:32 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:32 <10> Pos: -1 -1 Delta: -1 0
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:32 <10> Pos: -1 -1 Delta: 0 -1
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: 0 0
09.04.2009 00:29:32 <10> Pos: -2 -2 Delta: 0 -2
09.04.2009 00:29:32 <10> Pos: 0 0 Delta: 2 2
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:32 <10> Pos: 0 -1 Delta: 0 -1
09.04.2009 00:29:32 <10> Pos: -1 0 Delta: -1 0
09.04.2009 00:29:32 <10> Pos: -1 -1 Delta: 0 -1
09.04.2009 00:29:33 <10> Pos: -1 -1 Delta: 0 0
09.04.2009 00:29:36 <10> Pos: -1 -1 Delta: 0 0
09.04.2009 00:29:36 <10> Pos: -2 -2 Delta: 0 0
09.04.2009 00:29:36 <10> Pos: -5 -5 Delta: -3 -3
09.04.2009 00:29:36 <10> Pos: -5 -4 Delta: 0 1
09.04.2009 00:29:36 <10> Pos: -6 -6 Delta: -1 -2
09.04.2009 00:29:36 <10> Pos: -8 -7 Delta: -2 -1
09.04.2009 00:29:36 <10> Pos: -8 -7 Delta: 0 0
09.04.2009 00:29:36 <10> Pos: -14 -11 Delta: -6 -4
09.04.2009 00:29:36 <10> Pos: -20 -17 Delta: -6 -6
09.04.2009 00:29:36 <10> Pos: -26 -25 Delta: -6 -8
09.04.2009 00:29:36 <10> Pos: -33 -25 Delta: -7 0
09.04.2009 00:29:36 <10> Pos: -36 -31 Delta: -3 -6
09.04.2009 00:29:36 <10> Pos: -39 -31 Delta: -3 0
09.04.2009 00:29:36 <10> Pos: -38 -29 Delta: 1 2
09.04.2009 00:29:36 <10> Pos: -38 -24 Delta: 0 5
09.04.2009 00:29:36 <10> Pos: -33 -24 Delta: 5 0
09.04.2009 00:29:36 <10> Pos: -28 -21 Delta: 5 3
09.04.2009 00:29:36 <10> Pos: -27 -18 Delta: 1 3
09.04.2009 00:29:36 <10> Pos: -22 -16 Delta: 5 2
09.04.2009 00:29:36 <10> Pos: -19 -12 Delta: 3 4
09.04.2009 00:29:36 <10> Pos: -16 -11 Delta: 3 1
09.04.2009 00:29:36 <10> Pos: -14 -8 Delta: 2 3
09.04.2009 00:29:36 <10> Pos: -11 -8 Delta: 3 0
09.04.2009 00:29:36 <10> Pos: -8 -5 Delta: 3 3
09.04.2009 00:29:36 <10> Pos: -8 -5 Delta: 0 0
09.04.2009 00:29:36 <10> Pos: -4 -2 Delta: 4 3
09.04.2009 00:29:36 <10> Pos: -3 -3 Delta: 1 -1
09.04.2009 00:29:36 <10> Pos: -1 0 Delta: 2 3

3 个答案:

答案 0 :(得分:2)

我实际上无法回答您的问题,但您是否了解开源计划Synergy

它会做你想要做的事情 - 也许你可以看一下提示的来源。

答案 1 :(得分:1)

我不确定你要做什么的具体细节,但为什么不尝试将光标剪切到屏幕的一小部分并使其不可见。然后,每次检测到移动时,将客户端的光标移回到该剪切框的中心。所以你每次都在同一个屏幕上计算相对运动。

这就是PC模拟器的Bochs(几乎 - 它不会将光标剪切到窗口)。

答案 2 :(得分:0)

根据box方法得到我的解决方案。 MouseSimulator.Position就是这样做:

        Cursor.Position = new Point(value, Y);

HookManager_MouseMoveExt是我的回调。 MouseEventExtArgs只是扩展了MouseEventArgs,增加了增量等功能。

        private bool ignoreNext = false;
        void HookManager_MouseMoveExt(object sender, MouseEventExtArgs e)
        {
            // Should we block all mouse interactions?
            if (Block) {
                e.Handled = true;
                return;
            }

            // Return if we should ignore the nex, because we made a big jump
            if (ignoreNext) {
                ignoreNext = false;
                return;
            }

            if (hasControl) 
            {
                // Lock the mouse to 100,100 : 200,200 and flip back to 150,150 if out of bounds
                if (e.X < 100 || e.X > 200 || e.Y < 100 || e.Y > 200) // Box leaved
                {
                    // If we leave the box, we set the position to the center 
                    // and set the event to handled otherwise the mouse is free
                    MouseSimulator.Position = new System.Drawing.Point(150, 150);
                    e.Handled = true;
                    ignoreNext = true;
                }

                // We moved fine, send the delta to the server
                // The MouseSimulator.Position change will not be visible yet.
                server.MouseMove(e.DeltaX, e.DeltaY, true);

            }

            Logger.Log(String.Format("Pos: {0} {1} Delta: {2} {3}", e.X, e.Y, e.DeltaX, e.DeltaY), LogLevel.Info);
        }