iOS: How to detect the escape/control keys on a hardware bluetooth keyboard?

后端 未结 3 1656
南方客
南方客 2021-02-04 00:41

I\'m trying to figure out how to detect that the Escape (and other key combinations like Ctrl and alt) have been pressed on a bluetooth keyboard attached to an iOS device.

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-04 01:14

    You can do this now in iOS 7. For example, to implement the escape key, override UITextView and place the following methods in your class:

    - (NSArray *) keyCommands {
        UIKeyCommand *esc = [UIKeyCommand keyCommandWithInput: UIKeyInputEscape modifierFlags: 0 action: @selector(esc:)];
        return [[NSArray alloc] initWithObjects: esc, nil];
    }
    
    - (void) esc: (UIKeyCommand *) keyCommand {
        // Your custom code goes here.
    }
    

    You don't need to check to be sure you are on iOS 7, since earlier versions of the OS won't call the keyCommands method.

提交回复
热议问题