title: How to move the cursor or simulate clicks for other applications?
body: <p>I am creating a C# Windows application using the <a href="http://www.leapmotion.org/" rel="nofollow">Leap Motion Controller</a>. I am developing on Windows 8 and Visual Studio 2010. I use <code>SetCursorPos</code> and <code>mouse_event</code> from <code>user32.dll</code> to move the cursor and simulate clicks.</p>
  
  <p>I want the cursor to be moved when in any application. When I run/debug it from Visual Studio it only works when in the application itself or in Visual Studio. When in other applications the mouse does not move and the clicks don't work, but if I try to move the cursor with the real mouse it goes back to the place it was at. When run standalone it does not move in Visual Studio and the mouse can be moved with the real mouse in other applications.</p>
  
  <p>I have this code to use <code>SetCursorPos</code> and <code>mouse_event</code>:</p>
  
  <pre><code>[DllImport("user32.dll")]
  public static extern long SetCursorPos(int x, int y);
  
  [DllImport("User32.Dll")]
  public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
  
  public static void MouseClick(uint x, uint y) {
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
  }
  </code></pre>
  
  <p>In my form class I have the mouse location stored in two fields. In a <code>Timer</code>'s tick I set the cursor position and do necessary clicks like this:</p>
  
  <pre><code>if (!frame.Hands.IsEmpty) {
      Hand hand = frame.Hands.Leftmost;
      if (!hand.Fingers.IsEmpty) {
          // Get coordinates...
          SetCursorPos(mousex, mousey);
      }
  }
  foreach (Gesture gesture : gestures) {
      if (gesture.Type == Gesture.GestureType.TYPESCREENTAP) {
          MouseClick(mousex, mousey);
      }
  }
  </code></pre>
  
  <p>The <code>if</code> statements are for the Leap device; I want to move the mouse and do other stuff only when there is a hand to get the coordinates.</p>
  
  <p>Is it even possible to move the cursor or simulate mouse clicks for other applications? And if it is, how?</p>