How do you use CGEventTapCreate in Swift?

后端 未结 1 1334
独厮守ぢ
独厮守ぢ 2020-12-02 23:55

Has anyone managed to get this function to work in Swift?

Here is a reference SO post from last year: Using CGEventTapCreate Trouble with parameters in Swift

相关标签:
1条回答
  • 2020-12-03 00:50

    The callback parameter of CGEventTapCreate() is a C function pointer, and in Swift 1.x it is not possible call it with a Swift function argument.

    However, in Swift 2 (Xcode 7), C functions that take function pointer arguments can be called using closures or global functions (with the restriction that the closure must not capture any of its local context).

    As an example, here is a complete translation of Receiving, Filtering, and Modifying Key Presses and Releases to 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()
    

    Update for 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()
    
    0 讨论(0)
提交回复
热议问题