gpt4 book ai didi

objective-c - 如何创建 NSScrollWheel 类型的 NSEvent?

转载 作者:太空狗 更新时间:2023-10-30 03:19:08 25 4
gpt4 key购买 nike

我想构造(伪造)一个 NSScrollWheel 类型的 NSEvent。有一些其他类型的 NSEvent(mouseEvent、keyEvent、enterExitEvent、otherEvent)的 NSEvent 构造方法,但没有一个允许我设置,例如 deltaX。

最佳答案

NSEvent 不提供执行此操作的方法,但您可以创建一个 CGEvent 并围绕它创建一个 NSEvent 包装器。该事件将自动使用光标的当前位置。参见 CGEventCreateScrollWheelEvent .但是,使用 NSApplication 的 postEvent:atStart: 发布此事件不起作用(可能是因为它没有定义窗口)。您可以将 CGEvent 直接发送到事件流,或将 NSEvent 直接发送到 View 的 scrollWheel: 方法。

CGWheelCount wheelCount = 2; // 1 for Y-only, 2 for Y-X, 3 for Y-X-Z
int32_t xScroll = −1; // Negative for right
int32_t yScroll = −2; // Negative for down
CGEventRef cgEvent = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheelCount, yScroll, xScroll);

// You can post the CGEvent to the event stream to have it automatically sent to the window under the cursor
CGEventPost(kCGHIDEventTap, cgEvent);

NSEvent *theEvent = [NSEvent eventWithCGEvent:cgEvent];
CFRelease(cgEvent);

// Or you can send the NSEvent directly to a view
[theView scrollWheel:theEvent];

关于objective-c - 如何创建 NSScrollWheel 类型的 NSEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6126226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com