eonil
eonil

Reputation: 85975

How can I handle ESC key in Cocoa app?

I made an app switching to full screen mode. I want to use ESC key to escaping fullscreen mode, but binding menu item to ESC key in IB is removed at runtime. How can I keep ESC key binding to a menu item?

Upvotes: 23

Views: 12115

Answers (4)

phaxian
phaxian

Reputation: 1078

One way to capture keyboard events involves subclassing:

  1. Subclass your full-screen class (e.g.) NSView.
  2. Add the method - (void) keyDown:(NSEvent *)theEvent to the subclass implementation.
  3. Open up InterfaceBuilder and select the full-screen class that you previously created.
  4. Change its class to your new subclass.

The subclass looks something like:

MySubclass.h

@interface MySubclass : NSView {
}
@end

MySubclass.m

#import <Carbon/Carbon.h>
@implementation MySubclass
- (void)keyDown:(NSEvent *)theEvent
{       
    switch([theEvent keyCode]) {
        case kVK_Escape:
            NSLog(@"ESC");
                    // Call the full-screen mode method
            break;
        default:
            [super keyDown:theEvent];
    }
}
@end

This doesn't bind the ESC key to the menu item, but it does give you equivalent functionality (and a bit more flexability since you can intercept all keyboard events).

Upvotes: 11

eonil
eonil

Reputation: 85975

Preferred way to handle escape key in Cocoa is this as like @Josh Caswell said.

#pragma mark - NSResponder
- (void)cancelOperation:(id)sender
{
    [self exitFullScreen];
}

Upvotes: 52

Marek H
Marek H

Reputation: 5566

Many people try to implement esc key functionality. There is cancelOperation in the responder chain to handle escape events.

//WRONG
- (void)keyDown:(NSEvent *)event
{
    //unichar character = 0;
    //if ([event type] == NSEventTypeKeyDown) {
    //    if ([[event charactersIgnoringModifiers] length] == 1) {
    //        character = [[event characters] characterAtIndex:0];
    //    }
    //}

    switch (character) {
        //THIS IS WRONG correct is to implement interpretKeyEvents+moveRight 
        //case NSRightArrowFunctionKey:
        //    [self moveSelectedIndexRight];
        //    break;
        //THIS IS WRONG correct is to implement interpretKeyEvents+ moveLeft 
        //case NSLeftArrowFunctionKey:
        //    [self moveSelectedIndexLeft];
        //    break;
        //THIS IS WRONG correct is to implement interpretKeyEvents+ moveLeft 
        //case NSCarriageReturnCharacter:
        //    [self dismissWithCurrentlySelectedToken];
        //    break;
        default:
            [self interpretKeyEvents:@[event]];
            [super keyDown:event]
            break;
    }
}

//CORRECT
- (void)keyDown:(NSEvent *)event
{
   [self interpretKeyEvents:@[event]];
   [super keyDown:event];
}

`/* Catch the commands interpreted by interpretKeyEvents:. Normally, if we don't implement (or any other view in the hierarchy implements) the selector, the system beeps. Menu navigation generally doesn't beep, so stop doCommandBySelector: from calling up t`he hierarchy just to stop the beep.
*/
- (void)doCommandBySelector:(SEL)selector {
    if (   selector == @selector(moveRight:)
        || selector == @selector(moveLeft:)
        || selector == @selector(cancelOperation:)
        || selector == @selector(insertNewline:) )
    {
        [super doCommandBySelector:selector];
    }

    // do nothing, let the menu handle it (see call to super in -keyDown:)
    // But don't call super to prevent the system beep
}
- (void)cancelOperation:(id)sender
{
    //do your escape stuff
}

- (void)insertNewline:(id)sender
{
    //do your enter stuff
}

- (void)moveRight:(nullable id)sender
{
    [self moveSelectedIndexRight];
}

- (void)moveLeft:(nullable id)sender
{
    [self moveSelectedIndexLeft];
}

Upvotes: 2

slashlos
slashlos

Reputation: 933

I needed to dodge WKWebView crashes when ESC is pressed(?) so I sub-class it, and added:

import Carbon.HIToolbox

override func keyDown(with event: NSEvent) {
    if event.keyCode == UInt16(kVK_Escape) {
        //  We crash otherwise, so just close window
        self.window?.performClose(event)
    }
    else
    {
        // still here?
        super.keyDown(with: event)
    }
}

Upvotes: 0

Related Questions