I programmatically generate mouse clicks when a user presses a specific keyboard key (CapsLock). Thus, I do the left mouse button when CapsLock is turned on, and then the left mouse button when CapsLock is turned off.
This behaves correctly if, for example, you hover over the window title bar, click CapsLock, then move the mouse, and then click CapsLock, the window moves correctly. that is, I am correctly "dragging" the window, as if I held the left mouse button while moving the mouse.
However, there is one problem: the window does not move while I move the mouse, it only moves to the final position after I clicked CapsLock again. those. after I "released" the mouse button.
What do I need to do to make sure the screen is updated while moving the mouse?
Interestingly, I also connected to
[NSEvent addGlobalMonitorForEventsMatchingMask: NSLeftMouseDraggedMask
and found that my NSLog instruction was only displayed after I released the left mouse button (real left mouse button)
Below is the mouse click code, I can publish all the code, if necessary, it is not so much.
// 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);