首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Cocoa应用中处理ESC密钥?

如何在Cocoa应用中处理ESC密钥?
EN

Stack Overflow用户
提问于 2011-05-13 23:38:49
回答 3查看 11.3K关注 0票数 23

我做了一个切换到全屏模式的应用。我想使用ESC键来退出全屏模式,但是在运行时将菜单项绑定到IB中的ESC键被移除。如何将ESC键绑定到菜单项?

EN

回答 3

Stack Overflow用户

发布于 2011-05-14 14:53:54

在Cocoa中处理转义键的首选方法是类似于@Josh Caswell said

代码语言:javascript
复制
#pragma mark - NSResponder
- (void)cancelOperation:(id)sender
{
    [self exitFullScreen];
}
票数 50
EN

Stack Overflow用户

发布于 2017-10-16 17:53:07

许多人试图实现esc的关键功能。响应器链中有cancelOperation来处理转义事件。

代码语言:javascript
复制
//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];
}
票数 2
EN

Stack Overflow用户

发布于 2017-09-16 07:58:34

我需要在按ESC时躲避WKWebView崩溃(?)因此,我将其子类,并添加了:

代码语言:javascript
复制
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)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5994310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档