2009-04-08 136 views
2

我捕捉到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 

回答

0

得到了我的解決方案,基於箱子的方法。 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); 
     } 
1

我不知道,你正在試圖做什麼的細節,但爲什麼不嘗試裁剪光標移到屏幕的一小部分並使其不可見。然後每次檢測到移動時,將客戶端的光標移回該修剪框的中心。所以你每次都要從相同的屏幕點計算相對移動。

這就是PC模擬器Bochs所做的(幾乎 - 它不會將光標剪切到窗口)。

2

我實際上不能回答你的問題,但你知道開源程序Synergy

它做你正在做的事 - 也許你可以偷看提示的來源。

+0

我已經看過他們的代碼,但我不是很熟悉它看不到太多。我發現,他們跟蹤鼠標移動並使用定時器忽略錯誤的移動。但看起來很複雜。我希望有一個更簡單的方法 – Tarion 2009-04-08 22:35:13

相關問題