rptwsthi
rptwsthi

Reputation: 10172

Disable a particular key in UI Keyboard

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

Upvotes: 1

Views: 3288

Answers (4)

balajiaj
balajiaj

Reputation: 1

//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;
}

Upvotes: 0

Dip Dhingani
Dip Dhingani

Reputation: 2499

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.

Upvotes: 1

matm
matm

Reputation: 7079

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.

Upvotes: 1

Suhail Patel
Suhail Patel

Reputation: 13694

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

Upvotes: 4

Related Questions