【问题标题】:Simulate mouse wheel on macOS (swift or objc)在 macOS 上模拟鼠标滚轮(swift 或 objc)
【发布时间】:2018-07-25 12:48:00
【问题描述】:

我正在使用下面的代码来模拟鼠标点击事件

public class func leftMouseDown(onPoint point: CGPoint) {

    guard let downEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left) else {
        return
    }

    downEvent.setIntegerValueField(CGEventField.eventSourceUserData, value: 1)

    downEvent.post(tap: CGEventTapLocation.cghidEventTap)
}

public class func leftMouseUp(onPoint point: CGPoint) {

    guard let upEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left) else {
        return
    }

    upEvent.setIntegerValueField(CGEventField.eventSourceUserData, value: 1)

    upEvent.post(tap: CGEventTapLocation.cghidEventTap)
}

模拟鼠标滚轮滚动事件的类似方法是什么?

【问题讨论】:

  • 你查看CGEvent的文档了吗?

标签: objective-c swift macos simulation mousewheel


【解决方案1】:

感谢Willeke 从 10.13 开始添加了新的事件构造函数。

这是我的解决方案,但它仅适用于 macOS 版本 >= 10.13

public class func scrollMouse(onPoint point: CGPoint, xLines: Int, yLines: Int) {
    if #available(OSX 10.13, *) {
        guard let scrollEvent = CGEvent(scrollWheelEvent2Source: nil, units: CGScrollEventUnit.line, wheelCount: 2, wheel1: Int32(yLines), wheel2: Int32(xLines), wheel3: 0) else {
            return
        }
        scrollEvent.setIntegerValueField(CGEventField.eventSourceUserData, value: 1)
        scrollEvent.post(tap: CGEventTapLocation.cghidEventTap)
    } else {
        // scroll event is not supported for macOS older than 10.13
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多