I'm using Swift and I'm trying to figure out how to control my macOS mouse with my iPhone. I figured that the first steps would be to programmatically move the MacOS mouse with a macOS app. I'm not sure what I'm missing or doing wrong.
import Cocoa
import CoreGraphics
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
typealias CGDirectDisplayID = UInt32
func CGMainDisplayID() -> CGDirectDisplayID{
return CGMainDisplayID()
}
func CGDisplayMoveCursorToPoint(_ display: CGDirectDisplayID,
_ point: CGPoint){
}
self.CGDisplayMoveCursorToPoint(CGMainDisplayID(),(25,400))
}
I get an error: "Expected Declaration" for self.CGDisplayMoveCursorToPoint(CGMainDisplayID(),(25,400))
I have rewritten your class a little bit. This should accomplish what you want:
typealias CGDirectDisplayID = UInt32
class ViewController: NSViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
moveCursor(onDisplay: mainDisplayID(), toPoint: CGPoint(x: 25, y: 400))
}
public func mainDisplayID() -> CGDirectDisplayID
{
return CGMainDisplayID()
}
public func moveCursor(onDisplay display: CGDirectDisplayID, toPoint point: CGPoint)
{
CGDisplayMoveCursorToPoint(display, point)
}
}
The most important error in your current code is that your CGDisplayMoveCursorToPoint function has nothing inside its definition, so nothing will happen. Additionally, you don't want to name your functions like this since those are implemented by the Core Graphics framework and the compiler won't like that. The comments from Alexander seem to have helped you out with this.
Inside this custom moveCursor function you may call either CGDisplayMoveCursorToPoint(display, point) or CGWarpMouseCursorPosition(point) - they are both functionally equivalent, but one does not require a display ID. From there you need to call this moveCursor method inside one of your controller lifecycle methods. Alexander's suggestions are good to note, but for starting out and testing if everything works you can just put it inside viewDidLoad and the function will fire off as soon as the view is loaded.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With