Simulating mouse input programmatically in OS X

前端 未结 4 1305
悲&欢浪女
悲&欢浪女 2020-12-07 08:26

Is it possible to simulate the actions of a mouse from a program in OS X? Specifically, the short version is that I\'m trying to simulate a touchscreen using two webcams. So

4条回答
  •  生来不讨喜
    2020-12-07 08:26

    Swift mouse move and click example:

    func mouseMoveAndClick(onPoint point: CGPoint) { 
        guard let moveEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left) else {
            return
        }
        guard let downEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left) else {
            return
        }
        guard let upEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left) else {
            return
        }
        moveEvent.post(tap: CGEventTapLocation.cghidEventTap)
        downEvent.post(tap: CGEventTapLocation.cghidEventTap)
        upEvent.post(tap: CGEventTapLocation.cghidEventTap)
    }
    

提交回复
热议问题