美文网首页
MacCatalyst 键盘输入监听

MacCatalyst 键盘输入监听

作者: devileatapple | 来源:发表于2021-03-01 18:33 被阅读0次

    下面的方法放到AppDelegate里即可

    #pragma mark- 键盘监听

    -(void)pressesBegan:(NSSet *)presses withEvent:(UIPressesEvent*)event {

        /**

         // These are pre-defined constants for use with the input property of UIKeyCommand objects.

         UIKIT_EXTERN NSString *const UIKeyInputUpArrow        API_AVAILABLE(ios(7.0));

         UIKIT_EXTERN NSString *const UIKeyInputDownArrow      API_AVAILABLE(ios(7.0));

         UIKIT_EXTERN NSString *const UIKeyInputLeftArrow      API_AVAILABLE(ios(7.0));

         UIKIT_EXTERN NSString *const UIKeyInputRightArrow      API_AVAILABLE(ios(7.0));

         UIKIT_EXTERN NSString *const UIKeyInputEscape          API_AVAILABLE(ios(7.0));

         UIKIT_EXTERN NSString *const UIKeyInputPageUp          API_AVAILABLE(ios(8.0));

         UIKIT_EXTERN NSString *const UIKeyInputPageDown        API_AVAILABLE(ios(8.0));

         UIKIT_EXTERN NSString *const UIKeyInputHome            API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputEnd            API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF1              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF1              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF2              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF3              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF4              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF5              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF6              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF7              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF8              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF9              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF10            API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF11            API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         UIKIT_EXTERN NSString *const UIKeyInputF12            API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);

         */

        BOOL didHandleEvent=NO;

        for(UIPress*press in presses) {

            if(@available(macCatalyst13.4, *)) {

                UIKey*key=press.key;//键值

                //匹配键值

                if ([key.charactersIgnoringModifiers isEqualToString:UIKeyInputEscape]) {//esc

                    didHandleEvent=YES;

                  }

                if ([key.charactersIgnoringModifiers isEqualToString:UIKeyInputLeftArrow]) {//左箭头

                    didHandleEvent=YES;

                }

                if ([key.charactersIgnoringModifiers isEqualToString:UIKeyInputRightArrow]) {//右箭头

                    didHandleEvent=YES;

                }

            }else{

                // Fallback on earlier versions

            }

        }

        if(!didHandleEvent) {//没取到匹配值,调用父类

            [superpressesBegan:presseswithEvent:event];

        }

    }

    相关文章

      网友评论

          本文标题:MacCatalyst 键盘输入监听

          本文链接:https://www.haomeiwen.com/subject/cliafltx.html