-
https://developer.apple.com/documentation/coregraphics/cgevent
-
キー入力・マウス入力などのイベントは、I O Kitを通じてウィンドウサーバーのイベントキューに入り、ウィンドウサーバーはQuartz Eventを作成する。
-
Quartz EventはCarbon Event Managerに拾われて、適切なハンドラーに渡される
-
アプリケーションはEvent Tapを用いてアクセスできる
サンプルコード グローバルなキー入力イベントを取得する
import Foundation
import Cocoa
let eventMask: CGEventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue)
guard let eventTap = CGEvent.tapCreate(tap: .cgSessionEventTap, place: .headInsertEventTap, options: .defaultTap, eventsOfInterest: eventMask, callback: {(proxy, type, event, refcon) in
print("Key event \(type) received.")
return Unmanaged.passUnretained(event)
}, 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()