Command-W to close a Window

I've created a OS X (not iOS) applicaiton loosely based on http://practicalswift.com/2014/06/27/a-minimal-webkit-browser-in-30-lines-of-swift/.


As Takeshi says down in the comments, it would like to nice too allow the window to close with Command-W.


I came across this Objective-C recommandation from 2010 but it doesn't seem to apply: http://www.cocoawithlove.com/2008/12/drawing-custom-window-on-mac-os-x.html


What is needed to allow the popup window to close with Command-W? In Swift of course.


Thanks.


Ed

Got this to do the job for me:

    override func keyDown(theEvent: (NSEvent!))
    {
        if theEvent.modifierFlags.contains(.CommandKeyMask) {
            switch theEvent.charactersIgnoringModifiers! {
            case "w":
                self.window?.close()
            default:
                break
            }
        }
    }

Fun fact: your code is not the way a real app works. The reason you have to manually handle the Cmd-W event yourself is because for that Swift template, there is no menu bar (which, by the way, is really just a specially displayed NSMenu). Sure, you can see the name in the menu bar, but that's just a contingency feature—you cannot click the app's name to get a menu. That's why there is no keyboard shortcut processing.


Are you familiar with how the responder chain works?

Command-W to close a Window
 
 
Q