iosobjective-cuikeycommand

UIKeyCommands not working


I'm trying to add custom shortcuts for iPad to my app. However, with the code below I can't get to see them working. When I press the command key I get the discovery modal, but when I then press command+f nothing happens. What am I doing wrong?

#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (NSArray *)keyCommands {
    return @[
         [UIKeyCommand keyCommandWithInput:@"f" modifierFlags:0 action:@selector(backspacePressed:) discoverabilityTitle:@"New message"]
    ];
}

- (void)backspacePressed:(UIKeyCommand *)command {
    NSLog(@"Backspace key was pressed");
}

Solution

  • Turns out that, in order for me to be able to register cmd+f, I needed to change the modifierFlags from 0 to UIKeyModifierCommand.

    [UIKeyCommand keyCommandWithInput:@"f" modifierFlags:UIKeyModifierCommand action:@selector(backspacePressed:) discoverabilityTitle:@"New message"]