可可应用程序:以编程方式滚动?

Den*_*nis 5 cocoa nsevent

我目前正在开发一个适用于 iOS 和 macOS 的跨平台应用程序,使您的 iOS 设备充当触控板。

使用CGDisplayMoveCursorToPoint,我可以在屏幕上移动光标。奇迹般有效。通过“主机”应用程序(即在 macOS 上运行)中的这段代码,我可以单击鼠标:

let currentPosition = foo() // call to my helper function
                            // that translates NSEvent.mouseLocation()
let downEvent = CGEvent(
    mouseEventSource: nil,
    mouseType: .leftMouseDown, 
    mouseCursorPosition: currentPosition,
    mouseButton: .left
)
let upEvent = CGEvent(
    mouseEventSource: nil,
    mouseType: .leftMouseUp,
    mouseCursorPosition: currentPosition,
    mouseButton: .left
)
downEvent?.post(tap: CGEventTapLocation.cghidEventTap)
upEvent?.post(tap: CGEventTapLocation.cghidEventTap)
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。

现在我想在iOS设备上用两根手指实现滚动。问题不在于 iOS 和 macOS 之间的通信(顺便说一句,PeerTalk 很棒),而是我似乎无法在主机上触发滚动事件。

起初,我尝试了这样的事情:

let eventSource = CGEventSource(stateID: .hidSystemState)
let event = CGEvent(
    mouseEventSource: eventSource,
    mouseType: .scrollWheel,
    mouseCursorPosition: currentPosition,
    mouseButton: .left
)
Run Code Online (Sandbox Code Playgroud)

不幸的是,event总是这样nil,我不明白为什么。我想尝试的另一种方法是这样的(是的,我使用从全局事件监听器获得的硬编码值,用于测试目的):

NSEvent.mouseEvent(
    with: NSEventType.scrollWheel,
    location: NSPoint(x: 671.0, y: 568.0),
    modifierFlags: .init(rawValue: 0),
    timestamp: 198223.19430632601,
    windowNumber: 14389,
    context: nil,
    eventNumber: 0,
    clickCount: 1,
    pressure: 1.0
)
Run Code Online (Sandbox Code Playgroud)

但是我的应用程序在创建事件时崩溃了:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: _NSEventMask64FromType(type) & (MouseMask|NSEventMaskMouseMoved)'

有谁知道我如何实现滚动功能?提前致谢!

the*_*ski 5

从 MacOS 10.13 开始,CGEvent 有一个新的初始化程序,用于创建滚动事件:https://developer.apple.com/documentation/coregraphics/cgevent/2919739-init

我设法像下面这样使用它:

func PostMouseScrollEvent(up: Bool){
    var wheel1: Int32 = -100
    if up { wheel1 = 100 }
    let event = CGEvent(scrollWheelEvent2Source: nil, units: .pixel, wheelCount: 1, wheel1: wheel1, wheel2: 0, wheel3: 0)
    event?.post(tap: CGEventTapLocation.cghidEventTap)
}
Run Code Online (Sandbox Code Playgroud)


Den*_*nis 2

我解决了它

a) 使用桥接头:

#ifndef Header_h
#define Header_h
void createScrollWheelEvent();
#endif /* Header_h */
Run Code Online (Sandbox Code Playgroud)

b) 用普通的旧 C 语言编写一个函数来创建事件:

#include <stdio.h>
#include <ApplicationServices/ApplicationServices.h>

void createScrollWheelEvent() {
    CGEventRef upEvent = CGEventCreateScrollWheelEvent(
        NULL, 
        kCGScrollEventUnitPixel, 
        2, 1, 1 );
    CGEventPost(kCGHIDEventTap, upEvent);
    CFRelease(upEvent);
}
Run Code Online (Sandbox Code Playgroud)

c) 并直接从我的 Swift 代码调用它:

createScrollWheelEvent()
Run Code Online (Sandbox Code Playgroud)

CGEventCreateScrollWheelEvent由于函数的可变特性,在 Swift 中使用是不可能的。代码显然需要调整,但它应该能让偶然发现这篇文章的人了解如何解决它。

// 编辑

我还注意到这CGDisplayMoveCursorToPoint不是“正确的”。是的,它会移动光标,但是当我将光标放在扩展坞中的图标上时,没有弹出应用程序名称。因此,我切换到CGEvent移动光标。