Objective-C/Cocoa:检测所有按键

Oli*_*per 6 macos cocoa objective-c keypress

是否可以在每次用户按下键时运行方法.基本上我想在按下按键时在iPhone或iPad上运行声音.我不想在我的窗口或某个控件中检测按键,我想检测所有按下(例如当他们在Safari中输入什么时.我不需要知道密钥是什么.

谢谢

Tea*_*eak 11

使用CGEventTapCreate记录在这里:

https://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html

或者使用此处addGlobalMonitorForEventsMatchingMask:handler:记录的NSEvents :

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nsevent_Class/Reference/Reference.html

NSEvent示例:

[NSEvent addGlobalMonitorForEventsMatchingMask:(NSKeyDownMask) handler:^(NSEvent *event){
    [self keyWasPressedFunction: event];
    //Or just put your code here
}];
Run Code Online (Sandbox Code Playgroud)

我会说NSEvents更容易......

注意:

出于安全原因,Apple要求您在"系统偏好设置"中启用"启用辅助设备访问",以便使用以上方法的以太网.