Detect delete key using UIKeyCommand
Asked Answered
A

4

18

Anyone know how to detect the "delete" key using UIKeyCommand on iOS 7?

Alroy answered 27/2, 2014 at 12:18 Comment(0)
A
18

Simple really - need to look for the backspace character "\b"

Alroy answered 27/2, 2014 at 13:22 Comment(3)
This didn't work for me. It states "invalid escape sequence". So I changed it to "\\b" but this results in the method not getting called upon hitting "delete" on the Mac keyboard. UIKeyCommand(input: "\\b", modifierFlags: nil, action: "methodToCall:")Soupspoon
I found it. Use "\\" in Swift.Isidoro
@Joey try \u{08} instead of \\bPumping
M
33

As people were having problems with Swift, I figured a small, complete example in both Objective C and Swift might be a good answer.

Note that Swift doesn't have a \b escape character for backspace, so you need to use a simple Unicode scalar value escape sequence of \u{8}. This maps to the same old-school ASCII control character number 8 ("control-H", or ^H in caret notation) for backspace as \b does in Objective C.

Here's an Objective C view controller implementation that catches backspaces:

#import "ViewController.h"

@implementation ViewController

// The View Controller must be able to become a first responder to register
// key presses.
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (NSArray *)keyCommands {
    return @[
        [UIKeyCommand keyCommandWithInput:@"\b" modifierFlags:0 action:@selector(backspacePressed)]
    ];
}

- (void)backspacePressed {
    NSLog(@"Backspace key was pressed");
}

@end

And here's the equivalent view controller in Swift:

import UIKit

class ViewController: UIViewController {

    override var canBecomeFirstResponder: Bool {
        return true;
    }

    override var keyCommands: [UIKeyCommand]? {
        return [
            UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(backspacePressed))
        ]
    }

    @objc func backspacePressed() {
        NSLog("Backspace key was pressed")
    }

}
Mandragora answered 22/12, 2014 at 18:44 Comment(2)
Type 'UIKeyModifierFlags' has no member 'allZeros'Adhamh
@Adhamh I have now updated the code to work with the very latest version of Swift. Key differences: canBecomeFirstResponder and keyCommands are now overridden properties, not methods, modifierFlags is now an OptionSet so you can pass an empty set if there are no modifiers rather than use the allZeros kludge, and I'm using the new #selector syntax. Tested and working fine with an external keyboard.Mandragora
A
18

Simple really - need to look for the backspace character "\b"

Alroy answered 27/2, 2014 at 13:22 Comment(3)
This didn't work for me. It states "invalid escape sequence". So I changed it to "\\b" but this results in the method not getting called upon hitting "delete" on the Mac keyboard. UIKeyCommand(input: "\\b", modifierFlags: nil, action: "methodToCall:")Soupspoon
I found it. Use "\\" in Swift.Isidoro
@Joey try \u{08} instead of \\bPumping
P
0

As of iOS 15 there is a UIKeyCommand.inputDelete constant that works:

UIKeyCommand(title: "Delete Widget",
             action: #selector(WidgetViewController.deleteWidget(_:)),
             input: UIKeyCommand.inputDelete)
Panzer answered 7/6, 2023 at 10:21 Comment(0)
A
-2

You can always try UIKeyInput. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIKeyInput_Protocol/index.html#//apple_ref/occ/intfm/UIKeyInput/deleteBackward

The function should be

- (void)deleteBackward

Aprilaprile answered 22/12, 2014 at 19:7 Comment(1)
Implementing UIKeyInput presents a keyboard though when becoming first responder.Lyndy

© 2022 - 2024 — McMap. All rights reserved.