CGMouseMove doesn't trigger menu bar

Hey Guys,

I am writing a little Swift apllication, which runs on my Mac, that is connected to the TV. To control the mouse I use my own SmartHome-App. In this app I have implemented a touchpad like from the MacBook.

If the user triggers a tap/drag event, a UDP message will be sent to the mentioned application (MouseServer).

The MouseServer is listening for UDP messages and moves the mouse, when the command for mouse move was recieved.

Everything is working very well. But with the following mouse move code, I can't open the apple menu bar on top of the screen if I move the mouse to the top (when for example the browser is in fullscreen mode). I hope you know what I mean. If you are in a fullscreen window, the top menu bar within the apple icon disappears. But when you move the cursor to the top, the menu bar appears again. This doesn't work with my code. I've tried many different approches, but can't get it to work. Do you have any Idea?

func moveMouse(x: Int, y: Int) {
    // show cursor
    NSCursor.setHiddenUntilMouseMoves(false)
    NSCursor.unhide()
    
    // generate the CGPoint object for click event
    lastPoint = CGPoint(x: x, y: y)
    
    print("X: \(x), Y: \(y)")
    
    // --- Variant 1 ---
    // move the cursor to desired position
    CGDisplayMoveCursorToPoint(CGMainDisplayID(), lastPoint)
    CGDisplayShowCursor(CGMainDisplayID())
    
    // --- Variant 2 ---
    //if let eventSource = CGEventSource(stateID: .hidSystemState) {
    //    let mouseEvent = CGEvent(mouseEventSource: eventSource, mouseType: .mouseMoved, mouseCursorPosition: lastPoint, mouseButton: .left)
    //    mouseEvent?.post(tap: .cghidEventTap)
    //}
    
    // --- Variant 3 ---
    //moveMouseWithAppleScript(x: x, y: y)
}

func moveMouseWithAppleScript(x: Int, y: Int) {
    let script = """
        tell application "System Events"
            set mousePos to {\(x), \(y)}
            do shell script "caffeinate -u -t 0.1"
            do shell script "osascript -e \\"tell application \\\\\\"System Events\\\\\\"
                set position of mousePos to {item 1 of mousePos, item 2 of mousePos}
            end tell\\""
        end tell
    """
    
    let appleScript = NSAppleScript(source: script)
    var error: NSDictionary?
    appleScript?.executeAndReturnError(&error)
    
    if let error = error {
        print("Error executing AppleScript: \(error)")
    }
}

Best regards, Robin11

Replies

So you're saying that when a full screen app is frontmost, if you move the mouse cursor to the top of the screen directly, the menu bar appears, but if you place the cursor programmatically using CGDisplayMoveCursorToPoint the menu bar does not appear?

Are you also saying that the commented-out code which inserts a mouse-moved event doesn't work? If it doesn't, in what way does it not work - does it not move the cursor, or it does move the cursor but the menu bar doesn't appear as expected?

  • Yes, that's correct! If I move the mouse "manually" it works like a charm. But if I move the cursor programmatically the menu bar does not appear. There are also some other unusual behaviors like for example the mouse doesn't appear, when a video is playing while I move the cursor programmatically. Only when I "manually" move the mouse via trackpad. So I guess, that the CGDisplayMoveCursorToPoint does not fire all events, like the real cursor move does.

    And yes, all three variants, doesn't work

Add a Comment

Well, I'm out of ideas. If it were me, I'd file a DTS incident