iOS key code capture from external bluetooth scanner

白昼怎懂夜的黑 提交于 2019-12-03 22:47:10

I found out how to receive non-printable key codes from a bluetooth device connected to iOS in HID mode.

For reference, a 2D barcode has the general format:

[)><RS>'01'<GS>'9612345'<GS>'111'<GS>'000'<GS>'012345678901234'<GS>'FDEB'<GS><GS><GS><GS><GS>'25'<GS>'Y'<GS>'123 1ST AVE'<GS>'SEATTLE'<GS>'WA'<RS><EOT>

Where <RS> is char(30) or sequence ctrl-^, <GS> is char(29) or sequence ctrl-], and <EOT> is char(4) or ctrl-d, which are ASCII control codes.

In iOS 7 and above you can capture Key Down events from a HID bluetooth device using UIKeyCommand. UIKeyCommand is intended for capturing things like Command-A from a bluetooth keyboard, but it can also be used to map ASCII Sequences. The trick is to map the key code sequence as opposed to the ASCII code. For example in your view controller you can:

- (NSArray *) keyCommands {
    // <RS> - char(30): ctrl-shift-6 (or ctrl-^)
    UIKeyCommand *rsCommand = [UIKeyCommand keyCommandWithInput:@"6" modifierFlags:UIKeyModifierShift|UIKeyModifierControl action:@selector(rsKey:)];
    // <GS> - char(29): ctrl-]
    UIKeyCommand *gsCommand = [UIKeyCommand keyCommandWithInput:@"]" modifierFlags:UIKeyModifierControl action:@selector(gsKey:)];
    // <EOT> - char(4): ctrl-d
    UIKeyCommand *eotCommand = [UIKeyCommand keyCommandWithInput:@"D" modifierFlags:UIKeyModifierControl action:@selector(eotKey:)];
    return [[NSArray alloc] initWithObjects:rsCommand, gsCommand, eotCommand, nil];
}

- (void) rsKey: (UIKeyCommand *) keyCommand {
    NSLog(@"<RS> character received");
}

- (void) gsKey: (UIKeyCommand *) keyCommand {
    NSLog(@"<GS> character received");
}

- (void) eotKey: (UIKeyCommand *) keyCommand {
    NSLog(@"<EOT> character received");
}

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!