cocoa – 如果不将WebView添加到窗口中,则NSWindowController无法捕获ESC

cocoa – 如果不将WebView添加到窗口中,则NSWindowController无法捕获ESC,第1张

概述我从File-> New创建了NSWindowController的子类,并选中了“With XIB for user interface”选项.所以我创建了3个新文件.然后我使用Interface Builder将一个视图添加到窗口中. 并在MyWindowController.m中有这个代码: - (void)keyDown:(NSEvent *)theEvent{ NSLog(@"% 我从file-> New创建了NSWindowController的子类,并选中了“With XIB for user interface”选项.所以我创建了3个新文件.然后我使用Interface Builder将一个视图添加到窗口中.

并在MyWindowController.m中有这个代码:

- (voID)keyDown:(NSEvent *)theEvent{    NSLog(@"%@",theEvent);}

>首先测试,添加一个NSbutton,然后运行该项目.
>第二次测试,添加一个WebVIEw(NSbutton已删除),然后运行该项目.

在两个测试中,窗口都正确显示.但不同之处是:

>(NSbutton)当我按下’a’,’b’,…等键而不是ESC键时,我可以看到日志输出
>(WebVIEw)当我按下’a’,…和ESC键等键时,我可以看到日志输出

我将NSbutton更改为其他视图类型,以及我的自定义视图,所有 *** 作都像第一种情况一样.

我的气质是:

>为什么NSWindowController在第一种情况下无法捕获ESC键?
>为什么NSWindowController使用WebVIEw作为第一响应者捕获ESC键.
> NSWindowController如何在没有WebVIEw的情况下捕获ESC键?

解决方法 请参阅NSResponder cancelOperation:docs: https://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html

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:方法.

总结

以上是内存溢出为你收集整理的cocoa – 如果不将WebView添加到窗口中,则NSWindowController无法捕获ESC全部内容,希望文章能够帮你解决cocoa – 如果不将WebView添加到窗口中,则NSWindowController无法捕获ESC所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1002167.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存