gpt4 book ai didi

swift - 你如何在 Swift 中使用 CGEventTapCreate?

转载 作者:搜寻专家 更新时间:2023-10-30 22:22:10 28 4
gpt4 key购买 nike

有没有人设法让这个函数在 Swift 中工作?

这是去年的引用 SO 帖子:Using CGEventTapCreate Trouble with parameters in Swift

Apple 文档:https://developer.apple.com/library/prerelease/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/func/CGEventTapCreate

CGEventTapCallBack 的定义如下:

typealias CGEventTapCallBack = CFunctionPointer<((CGEventTapProxy, CGEventType, CGEvent!, UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>!)>

以下是我编写 block 的方式:

let eventTapCallBackBlock : @objc_block
(CGEventTapProxy, CGEventType, CGEventRef, UnsafeMutablePointer<Void>) -> CGEventRef =
{ (eventTapProxy: CGEventTapProxy, eventType: CGEventType, event: CGEventRef, refcon: UnsafeMutablePointer<Void>) in
return event
}

然后我用像 unsafeBitCast(eventTapCallBackBlock, CGEventTapCallBack.self) 这样的回调参数调用了 CGEventTapCreate

我得到一个有效的 CFMachPortRef 返回,但在运行时我在第一个事件中得到一个访问冲突异常。在当前发布状态下,我“似乎”接近于 swift 的解决方案。

使用 Xcode 版本 6.4

最佳答案

CGEventTapCreate()callback参数是一个C函数指针,在 Swift 1.x 中,无法使用 Swift 函数参数调用它。

但是,在 Swift 2 (Xcode 7) 中,接受函数指针参数的 C 函数可以使用闭包或全局函数调用(限制是闭包不得捕获其任何本地上下文)。

例如,这里是完整的翻译 Receiving, Filtering, and Modifying Key Presses and Releases swift :

import Foundation

func myCGEventCallback(proxy : CGEventTapProxy, type : CGEventType, event : CGEvent, refcon : UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>? {

if [.KeyDown , .KeyUp].contains(type) {
var keyCode = CGEventGetIntegerValueField(event, .KeyboardEventKeycode)
if keyCode == 0 {
keyCode = 6
} else if keyCode == 6 {
keyCode = 0
}
CGEventSetIntegerValueField(event, .KeyboardEventKeycode, keyCode)
}
return Unmanaged.passRetained(event)
}

let eventMask = (1 << CGEventType.KeyDown.rawValue) | (1 << CGEventType.KeyUp.rawValue)
guard let eventTap = CGEventTapCreate(.CGSessionEventTap,
.HeadInsertEventTap,
.Default,
CGEventMask(eventMask),
myCGEventCallback,
nil) else {
print("failed to create event tap")
exit(1)
}

let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes)
CGEventTapEnable(eventTap, true)
CFRunLoopRun()

Swift 3 的更新:

import Foundation

func myCGEventCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {

if [.keyDown , .keyUp].contains(type) {
var keyCode = event.getIntegerValueField(.keyboardEventKeycode)
if keyCode == 0 {
keyCode = 6
} else if keyCode == 6 {
keyCode = 0
}
event.setIntegerValueField(.keyboardEventKeycode, value: keyCode)
}
return Unmanaged.passRetained(event)
}

let eventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue)
guard let eventTap = CGEvent.tapCreate(tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: .defaultTap,
eventsOfInterest: CGEventMask(eventMask),
callback: myCGEventCallback,
userInfo: nil) else {
print("failed to create event tap")
exit(1)
}

let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes)
CGEvent.tapEnable(tap: eventTap, enable: true)
CFRunLoopRun()

关于swift - 你如何在 Swift 中使用 CGEventTapCreate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31891002/

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