Cocoa - the screen does not refresh after clicking the CGEventPost button

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); 
+4
source share
1 answer

If you want to drag windows, the problem is that you also need to place the LeftMouseDragged event.

Just call beginEventMonitoring to start listening for lock lock events and mouse move events. Event handlers will simulate left-clicking and moving the way you want. Here is a link to my blog where you can download the full working example for Xcode 4: http://www.jakepetroules.com/2011/06/25/simulating-mouse-events-in-cocoa

The example is in the public domain, do whatever you like with it. :)

According to Apple ( NSEvent documentation ), “Enable access for assistive devices” must be checked in “System Preferences”> “Universal Access” for this to work, but I have not verified it, and this is not a problem. Just a head.

Please let me know if you have any additional problems and I will try to help.

 // Begin listening for caps lock key presses and mouse movements - (void)beginEventMonitoring { // Determines whether the caps lock key was initially down before we started listening for events wasCapsLockDown = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, kVK_CapsLock); capsLockEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSFlagsChangedMask) handler: ^(NSEvent *event) { // Determines whether the caps lock key was pressed and posts a mouse down or mouse up event depending on its state bool isCapsLockDown = [event modifierFlags] & NSAlphaShiftKeyMask; if (isCapsLockDown && !wasCapsLockDown) { [self simulateMouseEvent: kCGEventLeftMouseDown]; wasCapsLockDown = true; } else if (wasCapsLockDown) { [self simulateMouseEvent: kCGEventLeftMouseUp]; wasCapsLockDown = false; } }]; mouseMovementEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSMouseMovedMask) handler:^(NSEvent *event) { [self simulateMouseEvent: kCGEventLeftMouseDragged]; }]; } // Cease listening for caps lock key presses and mouse movements - (void)endEventMonitoring { if (capsLockEventMonitor) { [NSEvent removeMonitor: capsLockEventMonitor]; capsLockEventMonitor = nil; } if (mouseMovementEventMonitor) { [NSEvent removeMonitor: mouseMovementEventMonitor]; mouseMovementEventMonitor = nil; } } -(void)simulateMouseEvent:(CGEventType)eventType { // Get the current mouse position CGEventRef ourEvent = CGEventCreate(NULL); CGPoint mouseLocation = CGEventGetLocation(ourEvent); // Create and post the event CGEventRef event = CGEventCreateMouseEvent(CGEventSourceCreate(kCGEventSourceStateHIDSystemState), eventType, mouseLocation, kCGMouseButtonLeft); CGEventPost(kCGHIDEventTap, event); CFRelease(event); } 
+7
source

All Articles