非NSResponder类捕捉按键消息
来源:互联网 发布:mac word 删除空白页 编辑:程序博客网 时间:2024/05/21 13:55
You may want to capture(or intercept, detect) the ESC key press in a Cocoa application, capture the ESC key down in Application scope for the whole app, or just in Window scope for some certain windows.
The NSApplicationDelegate is not a subclass of NSResponder, so you can not implement the keyDown: method int app delegate. The NSWindowController is a subclass of NSResponder, you may implement keyDown: method in it, but it will not get invoked when user press the ESC key(with exceptions) and not when user is typing in a text input.
The solution is to use a Event Monitor.
Here’s the sample codes:
1 @interface MyController : NSWindowController{ 2 id eventMonitor; 3 } 4 @end 5 6 @implementation MyController 7 - (void)windowDidLoad{ 8 NSEvent* (^handler)(NSEvent*) = ^(NSEvent *theEvent) { 9 NSWindow *targetWindow = theEven.window;10 if (targetWindow != self.window) {11 return theEvent;12 }13 14 NSEvent *result = theEvent;15 NSLog(@"event monitor: %@", theEvent);16 if (theEven.keyCode == 53) {17 [self myProcessEscKeyDown];18 result = nil;19 }20 21 return result;22 };23 eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:handler];24 }25 26 - (void)windowWillClose:(NSNotification *)notification{27 [NSEvent removeMonitor:eventMonitor];28 }@end
All the stuff is down in a NSWindowController subclass, if you want to implement a key capture in application scope, put these codes in the application delegate, replace windowDidLoad: withapplicationDidFinishLaunching:, windowWillClose: with applicationWillTerminate:.
- 非NSResponder类捕捉按键消息
- 非NSResponder类捕捉按键消息[How to capture ESC key in a Cocoa application]
- BlackBerry按键捕捉分析
- Unity 捕捉Android按键
- 捕捉界面中的按键
- VC++ VS2010 MFC CString 追加append 写入文件 txt 捕捉键盘按键消息
- 按键消息
- j2me-捕捉游戏操作按键
- ActivityGroup下捕捉按键事件
- spy++捕捉进程消息
- 捕捉键盘中断消息
- MIDLET下的按键捕捉问题
- js捕捉键盘事件和按键键值
- Form窗体中判断按键触发(捕捉)
- setOnKeyListener捕捉按键,接收两次的解决方法
- 鼠标滚轮消息的捕捉
- 鼠标滚轮消息的捕捉
- 鼠标滚轮消息的捕捉
- 在线转换工具
- MySQL索引使用的数据结构:B-Tree和B+Tree
- Python下访问MYSQL的方法总结
- 链表
- loadrunner性能测试脚本设计常用方法
- 非NSResponder类捕捉按键消息
- Andriod 详解View,ViewGroup的Touch事件的分发机制
- IDEA将maven项目配置到本地tomcat中运行
- hbase热点问题(数据倾斜)解决方案---rowkey散列和预分区设计
- JavaScript(js)对象的创建方法与使用
- 配置java、studio环境变量
- Python产生随机数(转)
- 自定义Mapper文件的几种写法
- 独立成分分析ICA系列5:信息极大化的 ICA 算法