使用带有 Swift 的 IOKit 进行简单的键重映射

Simple key remapping using IOKit with Swift

在 macOS 中,IOKit HID API 可用于键重映射。在下面的示例中,A 键被重新映射到 B 键,反之亦然,使用 Objective-C.

是否可以使用最新的 Swift 5.5 以编程方式进行相同的重新映射?如果可以的话,下面的例子在Swift中看起来如何重写?

或者是否有新的、更现代的 API 可以与 Swift 一起使用来完成相同的任务?


#import <Foundation/Foundation.h>
#import <IOKit/hidsystem/IOHIDEventSystemClient.h>
#import <IOKit/hidsystem/IOHIDServiceClient.h>
#import <IOKit/hid/IOHIDUsageTables.h>
 
int main(int argc, char *argv[])
{
    IOHIDEventSystemClientRef system;
    CFArrayRef services;
 
    uint64_t aKey = 0x700000004;
    uint64_t bKey = 0x700000005;
 
    NSArray *map = @[
                     @{@kIOHIDKeyboardModifierMappingSrcKey:@(aKey),
                        @kIOHIDKeyboardModifierMappingDstKey:@(bKey)},
                     @{@kIOHIDKeyboardModifierMappingSrcKey:@(bKey),
                        @kIOHIDKeyboardModifierMappingDstKey:@(aKey)},
                     ];
 
    system = IOHIDEventSystemClientCreateSimpleClient(kCFAllocatorDefault);
    services = IOHIDEventSystemClientCopyServices(system);
    for(CFIndex i = 0; i < CFArrayGetCount(services); i++) {
        IOHIDServiceClientRef service = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(services, i);
        if(IOHIDServiceClientConformsTo(service, kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard)) {
            IOHIDServiceClientSetProperty(service, CFSTR(kIOHIDUserKeyUsageMapKey), (CFArrayRef)map);
        }
    }
 
    CFRelease(services);
    CFRelease(system);
 
    return 0;
}

您可以像这样在 Swift 5.5 中重写您的诅咒代码:

import Cocoa

class ViewController: NSViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    
     remapKeys()
  }

  func remapKeys() {
    let aKey: UInt64 = 0x700000004
    let bKey: UInt64 = 0x700000005
    
    let map: [[String: UInt64]] = [
      [kIOHIDKeyboardModifierMappingSrcKey:aKey,
       kIOHIDKeyboardModifierMappingDstKey:bKey],
      [kIOHIDKeyboardModifierMappingSrcKey:bKey,
       kIOHIDKeyboardModifierMappingDstKey:aKey],
    ]
    
    let system = IOHIDEventSystemClientCreateSimpleClient(kCFAllocatorDefault)
    let services = IOHIDEventSystemClientCopyServices(system)
    
    for service in services as! [IOHIDServiceClient] {
      if((IOHIDServiceClientConformsTo(service, UInt32((kHIDPage_GenericDesktop)), UInt32(kHIDUsage_GD_Keyboard))) != 0) {
        IOHIDServiceClientSetProperty(service, kIOHIDUserKeyUsageMapKey as CFString, map as CFArray)
      }
    }
  }
}

假设您创建一个包含这些导入的桥接头:

#import <IOKit/hidsystem/IOHIDEventSystemClient.h>
#import <IOKit/hidsystem/IOHIDServiceClient.h>
#import <IOKit/hid/IOHIDUsageTables.h>