tags:

views:

174

answers:

1

hii i am new to iPhone world i am trying to trap the backspace key like if(string isEqualto:@"\b") but it doesn't work , the ascii code for backspace is 8 .. IS there any way to get the ascii code for the backscape key.... if possible plz tell me the code how to get the ascii value of character.

+4  A: 

Instead of getting the ASCII code for a backspace, think about using a delegate to trap the behavior you want. I know UITextFieldDelegate does what you want:

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

    if([string isEqualToString:@""]) {
        // Some replacement is taking place where the new string is empty
        // This implies a backspace (and not a character replacement)
        // Do your backspace-trapping here
        return NO;
    } else {
        return YES;
    }

}
Tim
Any particular reason why you're checking for an empty string rather than looking at range.length (0 for insert, 1 for delete)?
Stephen Darlington
Because it's not reliable with UITextField selection on iPhone OS 3.0+ - you can select a chunk of text and press a letter key to overwrite it with that letter (meaning `range.length` is greater than 0, but no backspace key was pressed).
Tim