Objective-C / Cocoa:检测所有按键 [英] Objective-C/Cocoa: Detect all keypresses
问题描述
每次用户按下一个键,都可以运行一个方法。基本上,我想在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
在此处记录:
或使用NSEvents addGlobalMonitorForEventsMatchingMask:handler:
在此处记录:
Or use NSEvents addGlobalMonitorForEventsMatchingMask:handler:
documented here:
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屋!