NSTokenField捕获一些NSEvents [英] NSTokenField catch some NSEvents

查看:148
本文介绍了NSTokenField捕获一些NSEvents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为NSTokenField和解决方案实施Command + Enter,Command + O和Esc弹出窗口,如 http://stackoverflow.com/a / 18486965/1067147 无效,因为 - (void)noop:(SEL)sel无效。

I need implement Command + Enter, Command + O and Esc shotcuts for NSTokenField and solutonns like http://stackoverflow.com/a/18486965/1067147 not worked because -(void)noop:(SEL)sel isn't useful.

推荐答案

My way is to create category for upper-in-hierarchy class NSView (also I try it for NSTextView but no luck):

//  NSView+WideInterpreter.h

#import <Cocoa/Cocoa.h>

#define kNotificationTokenModifier          @"kNotificationTokenModifier"
#define kNotificationTokenModifier_modifier @"kNotificationTokenModifier_modifier"

typedef enum{

    BMTokenModifier_CommandEnter = 10,
    BMTokenModifier_CommandO,
    BMTokenModifier_Esc,
    BMTokenModifier_nextKeyViewActivate

} BMTokenModifier;

@interface NSView (WideInterpreter)

@end

NSView+WideInterpreter.m
#import "NSView+WideInterpreter.h"

@implementation NSView (WideInterpreter)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

- (void)interpretKeyEvents:(id)keyEvents{

    NSMutableArray *result = [[NSMutableArray alloc] init];

    for (NSEvent *theEvent in keyEvents) {

        NSUInteger clearFlags = ([theEvent modifierFlags] &     NSDeviceIndependentModifierFlagsMask);
        BOOL commandPressed = (clearFlags == NSCommandKeyMask);

        switch ([theEvent keyCode]) {
            case 31:{//Ctrl+O
                if (commandPressed)
                    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTokenModifier
                                                                        object:self
                                                                          userInfo:@{kNotificationTokenModifier_modifier: @(BMTokenModifier_CommandO)}];
            }break;

            case 36:{//Enter
                if (commandPressed)
                    [[NSNotificationCenter defaultCenter]     postNotificationName:kNotificationTokenModifier
                                                                        object:self
                                                                          userInfo:@{kNotificationTokenModifier_modifier: @(BMTokenModifier_CommandEnter)}];
                else
                    [super insertNewlineIgnoringFieldEditor:self];
            }break;

            case 53://Esc
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTokenModifier
                                                                    object:self
                                                                  userInfo:@{kNotificationTokenModifier_modifier: @(BMTokenModifier_Esc)}];
                break;

            default:// allow super to handle everything else
                [result addObject:theEvent];
                break;
        }
    }

    [super interpretKeyEvents:result];

}

#pragma clang diagnostic pop

@end

对于使用工作:

[[NSNotificationCenter defaultCenter] addObserverForName:kNotificationTokenModifier
                                                  object:nil
                                                   queue:[NSOperationQueue currentQueue]
                                              usingBlock:^(NSNotification *note) {

                                                  BMTokenModifier modifier = (BMTokenModifier)[[[note userInfo] objectForKey:kNotificationTokenModifier_modifier] integerValue];

                                                  switch (modifier) {
                                                      case BMTokenModifier_CommandEnter:
                                                          [self sendMessage];
                                                          break;

                                                      case BMTokenModifier_CommandO:
                                                          [self attachFiles];
                                                          break;

                                                      default:
                                                          break;
                                                  }
                                              }];

这篇关于NSTokenField捕获一些NSEvents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆