user854211
user854211

Reputation:

How can I detect when the Backspace key is long pressed?

I'm working on an iOS project and I'm trying to detect when the backspace key is long pressed.

I'm using an UITextView filled with a lot of text and then I keep the backspace key pressed. First the text is removed char by char and then the whole text is deleted.

I'm using this method to detect all changes :

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

I observed that while the text is deleted char by char, this method is called, but when the whole text is deleted nothing happens.

Any thoughts on this ?

Thank you very much.

Upvotes: 0

Views: 1007

Answers (2)

Smit Shah
Smit Shah

Reputation: 209

This will detect backspace

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField==txtMobileNo)
    {

        const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
        int isBackSpace = strcmp(_char, "\b");

        if (isBackSpace == -8) {
            NSLog(@"isBackSpace");

            return YES; // is backspace
        }
        else if (textField.text.length == 10) {
            return YES;
        }
    }
    return NO;
}

Upvotes: 3

user470763
user470763

Reputation:

You could create a custom back key (or overlay a view on top of the standard one) and then use UITouch. So check when touchesBegan and start a timer which deletes a character from the string every x seconds. When touchedEnd kill the timer.

Upvotes: 0

Related Questions