Close\" menu item, which normally listens to the shortcut \"Command + W\"." />
开发者

Closing frontmost window in Cocoa in an app without a menu bar

I am building a StatusBar App in Cocoa, therefore I have no menu. Having no menu implies not having a "File > Close" menu item, which normally listens to the shortcut "Command + W".

From my StatusBar App the user may open a window to change the preferences and that's where I'm running into problems: The user can only close the window by pressing the red dot with the mouse. However, like alle applications I want to support the "Command + W" shortcut as well.

At the moment I see t开发者_如何转开发wo possibilities to solve this issue:

  1. Place an invisible button on the window which listens to the shortcut.
  2. Add an application-wide listener for the shortcut and contact the window manually.

Both solutions feel like a misuse of the system. The first solution can lead to unexpected behaviour (the window closes if the user hits the invisible button by chance) and the second solution will still result in a beep, since the window does not know that it handles such a shortcut.

Is there an elegant way to solve this problem? Since the view should know what to do, a solution with just Interface Builder would be perfect. If there is no elegant way, is there a way to enhance the solutions mentioned?

Thanks in advance!


If you put a File > Close menu item in your MainMenu nib, the shortcut will work, even though the menu isn't visible.

If you choose to implement an app-wide listener for the shortcut instead, you can get rid of the beep by returning nil from the block, so that the original event doesn't get passed on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜