title: Handle Cmd-Q in Cocoa Application (and Menu Item 'Quit Application' programmatically)
body: <p>I have created a gaming application that has only one window. Application is created without a help of .xib files as described here: <a href="https://stackoverflow.com/questions/656129/how-can-i-create-a-gui-and-react-to-cocoa-events-programatically">How can I create a GUI and react to Cocoa events programmatically?</a></p>
  
  <p>Now, I can catch standard 'key up/down' events in application's main loop:</p>
  
  <pre><code> NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
   NSEventType eventType = [event type];
   if (eventType == NSKeyDown)
   {
      my_uint32 keycode = [event keyCode];
      input::doSomeWork(keycode);
   }
  </code></pre>
  
  <p>Also, I can properly quit an application when a red cross is pressed on the window with the following code:</p>
  
  <pre><code>- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
  {
      g_myEngine.stop();
      return NSTerminateNow;
  }
  </code></pre>
  
  <p>But how do I:</p>
  
  <p>a) Catch when a menu item 'Quit MyApplicationName' is chosen?</p>
  
  <p>b) Handle Cmd-Q event?</p>
  
  <hr>
  
  <p>update:
  I have added this code:</p>
  
  <pre><code>id menubar = [[NSMenu new] autorelease];
  id appMenuItem = [[NSMenuItem new] autorelease];
  [menubar addItem:appMenuItem];
  [NSApp setMainMenu:menubar];
  id appMenu = [[NSMenu new] autorelease];
  id appName = [[NSProcessInfo processInfo] processName];
  id quitTitle = [@"Quit " stringByAppendingString:appName];
  id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
  [appMenu addItem:quitMenuItem];
  [appMenuItem setSubmenu:appMenu];
  </code></pre>
  
  <p>and now application exits from the menu, but Cmd-Q still does not work.</p>