On macOS, I have an application that should move the cursor to the center of the NSView when the window is loaded.
class ViewController: NSViewController {
override func viewDidLayout() {
super.viewDidLayout()
let view = super.view
let point = NSPoint(x: view.frame.midX, y:view.frame.midY)
let pointInWindow = view.convert(point, to: nil)
let pointOnScreen = view.window?.convertToScreen(NSRect(origin: pointInWindow, size: .zero)).origin ?? .zero
CGWarpMouseCursorPosition(NSPoint(x:pointOnScreen.x, y:pointOnScreen.y))
}
}
(The code was taken from: Mac OS X: Convert between NSView coordinates and global screen coordinates)
But it seems to be placing the cursor above the window instead of inside it— x-position looks correct but the y-position is off. Thank you for any help.