使用场景

ios项目通过catalyst支持Mac后,想要获取键盘事件,比如“tab”键,fn+F1,fn+F2,,,,,

如何使用

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (NSArray<UIKeyCommand *> *)keyCommands{
    UIKeyCommand *command = [UIKeyCommand commandWithTitle:@"shift+e" image:nil action:@selector(doKeyboardF1) input:@"e" modifierFlags:UIKeyInputF1 propertyList:nil];
    return @[command];
}

其他按键参考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];


注意点

keyCommandsUIResponder的方法,意味着重写的类一定是在事件响应链上的,如果没有触发,可以尝试

becomeFirstResponder

 

备注

部分按键、组合键在系统层被截断,无法监听,如F1~F12,command+c、command+v等。

无法区分按键的按下和松开



测试

Catalina,BigSur系统测试正常

 
版权声明:本文为IOS少年原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiyangxixia/p/16224026.html