searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

如何在 iOS 系统中监听外接键盘事件

2024-06-20 03:17:33
15
0

一、 使用 UIKeyCommand 监听键盘事件

在 iOS 中,监听键盘事件主要通过 UIKeyCommandUITextInput 协议来实现。UIKeyCommand 提供了一种简单的方法来监听特定的按键组合。

1、Declaration

@interface UIResponder (UIResponderKeyCommands)
@property (nullable,nonatomic,readonly) NSArray<UIKeyCommand *> *keyCommands NS_AVAILABLE_IOS(7_0); // returns an array of UIKeyCommand objects<
@end

2、Discussion

A responder object that supports hardware keyboard commands can redefine this property and use it to return an array of UIKeyCommand objects that it supports. Each key command object represents the keyboard sequence to recognize and the action method of the responder to call in response.

The key commands you return from this method are applied to the entire responder chain. When an key combination is pressed that matches a key command object, UIKit walks the responder chain looking for an object that implements the corresponding action method. It calls that method on the first object it finds and then stops processing the event.

3、示例代码

@interface CYExternalKeyboardTextView : UITextView
@end
​
@implementation CYExternalKeyboardTextView
​
- (NSArray<UIKeyCommand *> *)keyCommands {
  NSMutableArray *keys = [NSMutableArray new];
  //按键A
  [keys addObject:[UIKeyCommand keyCommandWithInput:@"a" modifierFlags:0 action:@selector(keyAction:)]];
  //按键shift, 按住的话会不停执行keyAction:
  [keys addObject:[UIKeyCommand keyCommandWithInput:@"" modifierFlags:UIKeyModifierShift action:@selector(keyAction:)]];
  
  //按键shift, 按住的话只执行一次keyAction
  {
      UIKeyCommand *onceShift = [UIKeyCommand keyCommandWithInput:@"" modifierFlags:UIKeyModifierShift action:@selector(keyAction:)]];
      [onceShift setValue:@(NO) forKey:@"repeatable"];
      [keys addObject:onceShift];
  }
  //组合键shift + A
  [keys addObject:[UIKeyCommand keyCommandWithInput:@"a" modifierFlags:UIKeyModifierShift action:@selector(keyAction:)]];
  //组合键ctrl + shift + A
  [keys addObject:[UIKeyCommand keyCommandWithInput:@"a" modifierFlags:UIKeyModifierControl | UIKeyModifierShift action:@selector(keyAction:)]];
  return keys;
  
}
- (void)keyAction:(UIKeyCommand *)keyCommand {
  //
}
@end

4、各按键对应input

上、下、左、右和esc

UIKIT_EXTERN NSString *const UIKeyInputUpArrow         NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputDownArrow       NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputLeftArrow       NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputRightArrow      NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputEscape          NS_AVAILABLE_IOS(7_0);

5、其他按键参考ASCII

//空格
NSString *space = [NSString stringWithFormat:@"%c",32];
//回车
NSString *enter = [NSString stringWithFormat:@"%c",13];
//Tab
NSString *tab = [NSString stringWithFormat:@"%c",9];
//1
NSString *one = [NSString stringWithFormat:@"%c",49];

6、备注

  1. 部分按键、组合键在系统层被截断,无法监听,如F1~F12,command+c等。
  2. 无法区分按键的按下和松开

二、 使用 GCKeyboard 监听键盘事件

在 iOS 14中,苹果推出了GameController框架,可通过GCKeyboard来更精确地监听键盘事件。

- (void)setupExternalKeyboard API_AVAILABLE(ios(14.0)) {
    GCKeyboardInput *keyboardInput = [[GCKeyboard coalescedKeyboard] keyboardInput];
​
    if (keyboardInput == nil) {
        LOG_ERROR(@"外接键盘:找不到键盘输入");
        return;
    }
​
    [keyboardInput setKeyChangedHandler:^(GCKeyboardInput * _Nonnull keyboard, GCControllerButtonInput * _Nonnull key, GCKeyCode keyCode, BOOL pressed) {
        if (pressed) {
            //keyCode按下
        } else {
            //keyCode松开
        }
    }];
}

三、 总结

在iOS14之后,推荐使用GCKeyboard监听键盘事件

0条评论
0 / 1000
程****斌
1文章数
0粉丝数
程****斌
1 文章 | 0 粉丝