title: Simulate Click and Drag in OS X
body: <p>I am trying to simulate mouse action via programming.</p>
  
  <p>I am not able to do <strong>click and drag action</strong> reliably.</p>
  
  <p>For click and drag I am doing the following actions in the order mentioned below</p>
  
  <ol>
  <li>Post left mouse button pressed event at current mouse pointer position (only once)</li>
  <li>Move the mouse (as mouse down event is already sent, it must act as mouse down and drag, selecting text from the initial mouse postion)</li>
  <li>Post left mouse button release event to end mouse hold action.</li>
  </ol>
  
  <p>In apple applications such as Xcode, or Safari, this method does not work reliably (It docent highlight text when dragged, but with actual mouse I can highlight text by click and drag). Cannot drag icons either.</p>
  
  <p>However in <strong>Firefox</strong>, this technique works. The selection follows mouse! That is as we move mouse pointer, the selection also changes accordingly, which is what I need. </p>
  
  <p>The following is the code I use to send mouse tap events.   </p>
  
  <pre><code>-(void)_sendMouseEvent:(int)mouseEvent withMouseButton:(CGMouseButton)mouseBtn
  {
      CGEventRef ourEvent = CGEventCreate(NULL);
      NSPoint mouseLoc  = CGEventGetLocation(ourEvent); //get current mouse position
  
      CGEventRef mouseClick = CGEventCreateMouseEvent(
                                                      NULL,
                                                      mouseEvent,
                                                      mouseLoc,
                                                      mouseBtn
                                                      );
      CGEventPost(kCGHIDEventTap, mouseClick);
      CFRelease(ourEvent);
      CFRelease(mouseClick);
  }
  </code></pre>
  
  <p>I did try sending mouse down events at regular intervals, but it made no change.</p>
  
  <p>Do you have any ideas on what I am doing wrong? Please suggest any workarounds that come to your mind. </p>