title: cocoa - screen not refreshing after CGEventPost mouse click
body: <p>I am programatically generating mouse clicks when a user clicks a certain keyboard key (CapsLock).
  So I do a left mouse down when CapsLock is switched on, then a left mouse up when CapsLock is switched off.</p>
  
  <p>This behaves correctly in that if I for example place the mouse over a window title bar, click CapsLock, then move the mouse, then click CapsLock, the window correctly moves. i.e. I correctly 'drag' the window as if I had held the left mouse button down whilst moving the mouse.</p>
  
  <p>However there is one problem - the window does not move whilst I am moving the mouse, it only moves to the final position after I have clicked CapsLock a second time. i.e. after I have 'released' the mouse button.</p>
  
  <p>What do I need to do to ensure the screen is refreshed during the mouse move?</p>
  
  <p>Interestingly, I also hooked to </p>
  
  <p>[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask </p>
  
  <p>and found that my NSLog statement only output <em>after</em> I released the left mouse button (the real left mouse button)</p>
  
  <p>Mouse click code is below, I can post all the code if necessary, there isn't much of it..</p>
  
  <pre><code>// simulate mouse down
  
  // get current mouse pos
  CGEventRef ourEvent = CGEventCreate(NULL);
  CGPoint point = CGEventGetLocation(ourEvent);
  NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
  
  CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
  CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
  CGEventSetType(theEvent, kCGEventLeftMouseDown);
  CGEventPost(kCGHIDEventTap, theEvent);
  CFRelease(theEvent);
  
  
  // simulate mouse up
  
  // get current mouse pos
  CGEventRef ourEvent = CGEventCreate(NULL);
  CGPoint point = CGEventGetLocation(ourEvent);
  NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
  
  CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
  CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseUp, point, kCGMouseButtonLeft);
  CGEventSetType(theEvent, kCGEventLeftMouseUp);
  CGEventPost(kCGHIDEventTap, theEvent);
  CFRelease(theEvent);
  </code></pre>