没有将WebView添加到窗口中,NSWindowController无法捕获ESC [英] NSWindowController can't capture ESC without WebView is added to the window
问题描述
我从File-> New创建一个NSWindowController的子类,并选中了 With XIB for user interface选项。因此,我创建了3个新文件。然后,我使用Interface Builder将一个视图添加到窗口中。
I create a subclass of NSWindowController from File->New, and with the "With XIB for user interface" option checked. So I create 3 new files. Then I use Interface Builder to add exactly ONE view into the window.
并将此代码保存在MyWindowController.m中:
And have this code in MyWindowController.m:
- (void)keyDown:(NSEvent *)theEvent{
NSLog(@"%@", theEvent);
}
- 第一次测试,添加一个NSButton并运行
- 第二项测试,添加一个WebView(已删除NSButton),然后运行该项目。
在两个测试中,窗口均正确显示。但是区别是:
In both tests, the window shows up correctly. But the diffrence is:
- (NSButton)当我按'a','b',..等键时,我可以看到日志输出。 。,但没有ESC键
- (WebView)当我按下 a, b,...,等键和ESC键时,我可以看到日志输出
- (NSButton)I can see log output when I press keys like 'a', 'b', ..., but not the ESC key
- (WebView)I can see log output when I press keys like 'a', 'b', ..., and the ESC key as well
我将NSButton更改为其他视图类型,以及我的自定义视图,都像第一种情况一样。
I change NSButton to other view type, and also my custom view, all act like the first case.
我的问题是:
- 为什么NSWindowController无法捕获ESC键
- 为什么NSWindowController使用WebView作为第一响应者向下捕获ESC键。
- NSWindowController如何在没有WebView的情况下捕获ESC键?一个WebView?
推荐答案
请参见 NSResponder
cancelOperation:
文档: https:// devel oper.apple.com/library/mac/ipad/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html
此方法绑定到Escape和Command-。 (句号)键。
键窗口首先在视图层次结构中搜索一个等效于其键
为Escape或Command-。的视图。如果
个视图中的任何一个都不处理等效键,则该窗口将默认的
操作消息cancelOperation:发送给第一个响应者,然后消息从
沿响应者链向上传播。如果
中没有响应者,则响应者链实现cancelOperation:,键窗口
在视图层次结构中搜索键等效项为Escape
的视图(请注意,如果原始键等效项可能是多余的是
转义)。如果没有找到这样的响应者,则将取消:操作消息
发送到实现
的响应者链中的第一个响应者。
This method is bound to the Escape and Command-. (period) keys. The key window first searches the view hierarchy for a view whose key equivalent is Escape or Command-., whichever was entered. If none of these views handles the key equivalent, the window sends a default action message of cancelOperation: to the first responder and from there the message travels up the responder chain. If no responder in the responder chain implements cancelOperation:, the key window searches the view hierarchy for a view whose key equivalent is Escape (note that this may be redundant if the original key equivalent was Escape). If no such responder is found, then a cancel: action message is sent to the first responder in the responder chain that implements it.
如果要在 NSWindowController
子类中处理Esc键,只需定义一个 cancel:
方法。
And if you want to handle Esc key in your NSWindowController
subclass just define a cancel:
method in it.
这篇关于没有将WebView添加到窗口中,NSWindowController无法捕获ESC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!