• Mac开发如何处理键盘事件


    Mac上输入与手机输入的不同是,Mac需要处理更多的键盘交互,因为Mac上的键盘输入会有多种快捷键组合。

    代理方法处理

    NSTextField

    #pragma mark - NSTextFieldDelegate
    - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
    {
        if ([NSStringFromSelector(commandSelector) isEqualToString:@"insertNewline:"]) {
            
            if (([NSApplication sharedApplication].currentEvent.modifierFlags & NSEventModifierFlagShift) != 0) {
                NSLog(@"Shift-Enter detected.");
                [textView insertNewlineIgnoringFieldEditor:self];
                return YES;
            }else {
                NSLog(@"Enter detected.");
            }
        }
        return NO;
    }
    

    NSTextView

    #pragma mark - NSTextViewDelegate
    - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
    {
        //insertTab:   -键入tab
        //insertNewline:   -键入回车
        //deleteBackward
        
        if ([NSStringFromSelector(commandSelector) isEqualToString:@"insertNewline:"]) {
            
            if (([NSApplication sharedApplication].currentEvent.modifierFlags & NSEventModifierFlagShift) != 0) {
                NSLog(@"Shift-Enter detected.");
                [textView insertNewlineIgnoringFieldEditor:self];
                return YES;
            }else {
                NSLog(@"Enter detected.");
                return YES;
            }
        }
        return NO;
    }
    

    通过监听键盘事件处理

    [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {
            [self keyDown:aEvent];
            return aEvent;
        }];
        
    

    然后重写keyDown方法

    - (void)keyDown:(NSEvent *)event{
        NSLog(@"%@",event.characters);
        if ([event.characters isEqualToString:@"
    "]) {
            NSLog(@"Enter detected.");
        } else {
            
        }
    }
    

    补充 - 格式化键盘输入

    OttoCustomFormatter一般用来处理纯数字输入以及密码输入

    使用方法

    self.inputTF.formatter = [[OttoCustomFormatter alloc] init];
    

    代码实现

    OttoCustomFormatter.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef enum : NSUInteger {
        OttoCustomFormatterTypeNumber,//纯数字
        OttoCustomFormatterTypePassword,//密码
    } OttoCustomFormatterType;
    
    @interface OttoCustomFormatter : NSFormatter
    
    /**
     *  可输入的最大长度 默认为8
     */
    @property (nonatomic,assign) NSInteger maxLength;
    
    /**
     *  格式化类型  默认为纯数字类型
     */
    @property (nonatomic,assign) OttoCustomFormatterType formatterType;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    OttoCustomFormatter.m

    #import "OttoCustomFormatter.h"
    
    @implementation OttoCustomFormatter
    
    - (instancetype)init{
        self = [super init];
        if (self) {
            self.maxLength = 8;
            self.formatterType = OttoCustomFormatterTypeNumber;
        }
        return self;
    }
    
    - (NSString *)stringForObjectValue:(id)obj{
        return (NSString *)obj;
    }
    
    - (BOOL)getObjectValue:(out id  _Nullable __autoreleasing *)obj forString:(NSString *)string errorDescription:(out NSString * _Nullable __autoreleasing *)error{
        *obj = string;
        return YES;
    }
    
    - (BOOL)isPartialStringValid:(NSString * _Nonnull __autoreleasing *)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString * _Nullable __autoreleasing *)error{
        if([*partialStringPtr length] == 0)
        {
            return YES;
        }
        if([*partialStringPtr length] > self.maxLength)
        {
            return NO;
        }
        NSRange passwordRange;
        NSString *strLetterDigital;
        if (self.formatterType == OttoCustomFormatterTypeNumber) {
            strLetterDigital = @"1234567890";
        } else if (self.formatterType == OttoCustomFormatterTypePassword) {
            strLetterDigital = @"1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        }
        NSString *strPassword;
        //使用characterSetWithCharactersInString初始化字符集
        NSCharacterSet *charactersetDigitAndLetter =[NSCharacterSet characterSetWithCharactersInString:strLetterDigital];
        charactersetDigitAndLetter = [charactersetDigitAndLetter invertedSet];
        passwordRange =NSMakeRange(origSelRange.location,
                                    proposedSelRangePtr->location
                                    - origSelRange.location);
        strPassword= [*partialStringPtr substringWithRange: passwordRange];
        if([strPassword rangeOfCharacterFromSet: charactersetDigitAndLetter
                                           options:NSLiteralSearch].location!=NSNotFound)
        {
            *error =@"Illegal input";
            return(NO);
        }else
        {
            *error =nil;
            return(YES);
        }
    }
    
    @end
    
  • 相关阅读:
    【leetcode】二叉搜索树中的众数
    【leetcode】和为零的N个唯一整数
    【leetcode】统计位数为偶数的数字
    【leetcode】寻找数组的中心索引
    【leetcode】找出给定方程的正整数解
    【leetcode】寻找比目标字母大的最小字母
    【leetcode】找出井字棋的获胜者
    输出0~N之间的素数
    判断正整数是否是素数(数据结构课后练习题2.13a)
    052-85
  • 原文地址:https://www.cnblogs.com/wuotto/p/12083569.html
Copyright © 2020-2023  润新知