gpt4 book ai didi

swift - 以正确的方式创建 CGEvent tap

转载 作者:行者123 更新时间:2023-11-28 06:09:01 25 4
gpt4 key购买 nike

我正在尝试学习如何使用 SO 和其他地方的示例创建 CGEvent tap。

我知道如何使用以下方法创建事件点击:

let eventTap = CGEvent.tapCreate(*/arguments to create the tap*/)

然后我跑

let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes)

我在网上找到的一些但不是所有的例子也添加了以下两行

CGEvent.tapEnable(tap: eventTap, enable: true)
CFRunLoopRun()

有人可以简要解释添加最后两行是否或何时有用/推荐吗?

最佳答案

第一个很简单:the docs说:

Event taps are normally enabled when created. If an event tap becomes unresponsive, or if a user requests that event taps be disabled, then a kCGEventTapDisabled event is passed to the event tap callback function. Event taps may be re-enabled by calling this function.

第二个可能适用于程序没有 run loop 的情况。 (在当前线程上)。例如,不使用 NSApplication 的命令行程序将需要创建并运行自己的运行循环。

关于swift - 以正确的方式创建 CGEvent tap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47265452/

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