views:

154

answers:

2

I'm trying to bind a bare esc key press to an NSMenuItem that toggles full screen (currently just a stub function). Manually selecting the menu item sends the desired IBAction. When I set the NSMenuItem's Key Equiv. in Interface Builder to an arbitrary bare key (eg. w) that key command sends the desired IBAction. When I set the Key Equiv. to command + esc, that key command sends the desired IBAction. But a bare esc key press is ignored.

I'm assuming the esc key is special-cased. Other applications (eg. Bannister's various emulators) are able to achieve this, any idea how?

A: 

I'm no Objective-C veteran, so apologies if I'm misunderstanding the question. But have you tried moving up the responder chain and grabbing the keyDown event in NSWindow? Something like:

- (void)keyDown: (NSEvent *) event {
   if ([event keyCode] == 53) {
        NSLog(@"Esc. pressed");
     }
}

Of course, this solution will require that you subclass NSWindow.

ndg
Thanks Nial. I accepted this because it solves the general use problem. Unfortunately it only solves half of my specific problem (which I glossed over in my question). I'm using enterFullScreenMode:withOptions: to enter full screen mode which doesn't use my subclassed NSWindow. This solution allows me to enter but not exit full screen mode. Apple's GLFullScreen example code should get me the rest of the way. <http://developer.apple.com/mac/library/samplecode/GLFullScreen/Introduction/Intro.html>
Shaun Inman
Unless I'm mistaken, it should just be a case of implementing keyDown: within your NSView. Failing that, this may be of interest: http://www.mail-archive.com/[email protected]/msg03851.html
ndg
Implementing keyDown: in my NSOpenGLView subclass worked. This still all seems like arbitrary magic...Thanks!
Shaun Inman
A: 

Please have a look at http://developer.apple.com/mac/library/documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGUserInput/XHIGUserInput.html “… The Esc (Escape) key basically means “let me out of here.” It has specific meanings in certain contexts. The user can press Esc in the following situations: …”

Greetings

Ojective Interested Person