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