2

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

但是,如果我阻止发送鼠标坐标(客户端)的屏幕上的鼠标移动或到达屏幕的一侧,则 Windows api 会触发第二个鼠标事件,该事件会将鼠标弹回。

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

首先我打电话:

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
4

3 回答 3

2

我实际上无法回答您的问题,但是您知道开源程序Synergy吗?

它会做你想做的事情——也许你可以看看源代码以获取提示。

于 2009-04-08T22:20:33.353 回答
1

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

这就是 PC 模拟器 Bochs 所做的(几乎——它不会将光标剪辑到窗口上)。

于 2009-04-08T21:46:09.927 回答
0

得到了我的解决方案,基于盒子方法。MouseSimulator.Position 只是做:

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

HookManager_MouseMoveExt 是我的回调。MouseEventExtArgs 只是扩展的 MouseEventArgs,添加了诸如 deltas 之类的功能。

        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);
        }
于 2009-04-08T23:02:11.443 回答