Objective-C / Cocoa:检测所有按键 [英] Objective-C/Cocoa: Detect all keypresses

查看:202
本文介绍了Objective-C / Cocoa:检测所有按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to run a method every time the user presses a key. Basically I want to run a sound like on the iPhone or iPad when a key is pressed. I do not want to detect key presses in my window or in a certain control, I want to detect ALL presses (such as when they are typing in Safari or something. I do not need to know what the key is.

感谢

推荐答案

使用 CGEventTapCreate 在此处记录:

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

或使用NSEvents addGlobalMonitorForEventsMatchingMask:handler:在此处记录:

Or use NSEvents addGlobalMonitorForEventsMatchingMask:handler: documented here:

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

NSEvent示例:

NSEvent Example:

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

我想说NSEvents更容易...

I would say NSEvents are easier...

注意:

出于安全考虑,Apple要求您在系统偏好设置中启用启用辅助设备的访问权限,以便使用上述方法。

For security reasons, Apple requires you have "Enable access for assistive devices" turned on in System Preferences, in order to use ether of the above methods.

这篇关于Objective-C / Cocoa:检测所有按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆