如何使用 *ENTER 或 ESC 键关闭 OS X 模态 sheet

How to dismiss an OS X modal sheet with *either* ENTER or ESC key

我的应用程序启动了一个有多个按钮的模式 sheet。其中之一是默认按钮(等效键 \r),按键盘上的 ENTER 键会按预期解除 sheet。如果用户按下 ESC 键,我也想达到同样的效果。所以 either ENTERESC 应该是按钮的等效键。如何实现?

-[NSResponder cancelOperation:],自动绑定转义键:

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.

换句话说,您可以通过在应用的响应链中的某处实现 -cancelOperation: 来处理转义键,例如 window 控制器。