下面是我的简单 AppDelegate.swift 类。
// AppDelegate.swift
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSEvent.addGlobalMonitorForEventsMatchingMask(NSEventMask.KeyDownMask, handler: keyDown);
}
func keyDown(event:NSEvent!) {
print("key down is \(event.keyCode)");
}
}
为什么按键按下事件没有被打印出来?我错过了什么? 我试过四处搜索,但无法弄清楚。
最佳答案
根据 addGlobalMonitorForEventsMatchingMask:handler:
上的 NSEvent.h
:
Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted in AXUIElement.h). Note that your handler will not be called for events that are sent to your own application.
关于 swift NSEvent 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34244865/