1

It need to disable '&' key from number and punctuation keyboard, so is it possible to disable a particular key in UIKeyboard?

rptwsthi
  • 10,094
  • 10
  • 68
  • 109

4 Answers4

4

I don't think it's possible to disable a certain key (unless it's one of the action keys such as the return key) but if you are using a UITextField you can use the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string delegate method to see if the user pressed the & key and remove it from the string

Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
1

You cannot do that. However your options are:

  • create your own custom keyboard not offerring '&' key (too much effort IMO)
  • If you use UITextField you can validate the text submitted by user: remove '&' and/or inform user that it is not allowed to use '&' (much easier).

EDIT: you can also connect UITextField's "Editing Changed" event to the File's Owner's IBAction and filter out '&' there.

matm
  • 7,059
  • 5
  • 36
  • 50
1

There is one delegate method for textField in which you can block specific characters if you want based on their ASCII values. The method can be written as follows:

-(BOOL)keyboardInput:(id)k shouldInsertText:(id)i isMarkedText:(int)b 
{
char s=[i characterAtIndex:0];
  if(selTextField.tag==1)
    {
        if(s>=48 && s<=57 && s == 38)  // 48 to 57 are the numbers and 38 is the '&' symbol
        {
            return YES;
        }
         else
        {
            return NO;
        }

    }
}

This method will permit only numbers and & symbol to be entered by the user. Even if the user presses other characters they won't be entered. And as it is a textField's delegate method you don't need to worry about calling it explicitly.

Dip Dhingani
  • 2,499
  • 2
  • 20
  • 18
0
//Disabling the '<' '>' special characters key in Keyboard in my code

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSCharacterSet *nonNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
    if (range.length == 1)
        return YES;
    else
        return ([text stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
    return YES;
}
CRUSADER
  • 5,486
  • 3
  • 28
  • 64