5

HI I want to detect tab button pressed in iPad app from wireless keyboard(external keyboard) So I make next textfield first responder. I am doing the following code, But it not working on tab key. It working fine incase return key.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(textField == textName){      
        if ([string isEqualToString:@"\t"]) {
            [textName resignFirstResponder];
              [textRno becomeFirstResponder];   
        }
    }
    return YES;
}

Is there any other way for it.

mandeep-dhiman
  • 2,505
  • 2
  • 21
  • 31

7 Answers7

5

Seems you want to switch to the next input view (e.g. text field) on tab stroke. The system does this automatically!

You can control the "tab-order" using the view tags and the code in this answer: https://stackoverflow.com/a/1351090/550177

Also look at these related questions:

Community
  • 1
  • 1
Felix
  • 35,354
  • 13
  • 96
  • 143
  • I have a very complex view and for some reason the system does *not* switch to the next text field automatically. I have added 'next' and 'prev' buttons to do that, which work fine. I just want to call my 'next' code when the user taps the Tab key. What happens now when the user taps the Tab key is the current text field loses focus and that's it. – Elijah Jul 31 '12 at 18:47
2

TextField delegate never get called for a tab button on the keyboard..

iOS Monster
  • 2,099
  • 1
  • 22
  • 49
  • To clarify, I think @iPhoneMonster is referring to this - http://stackoverflow.com/questions/4036510/ignore-tab-character-in-uitextfield-ipad-app. – Perception Jul 18 '11 at 13:22
  • @Perception: Sorry friend but I haven't googled this question...I tried this in my testApp to check whether it works or not...If it's available on the link you gave here then its even better for others..!! – iOS Monster Jul 19 '11 at 04:11
1

you can catch the \t escape char when you pressed on your external keyboard:

- the alt + tabulator or

- the ctrl + tabulator.

to press the tabulator button only does not cause any effect as you've already experienced because when the tabulator button is pressed, your application won't get any feedback about this event, therefore you cannot handle when the user presses this button only.

Community
  • 1
  • 1
holex
  • 23,961
  • 7
  • 62
  • 76
1

This is how I switch to the next responder with the tab key:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (self.currentTextField != nil && self.currentTextField == textField)
        [self switchToNextResponder];

    self.currentTextField = textField;
    return YES; 
}
grigorievs
  • 162
  • 6
0

use a UIEvent approach at here

Allen
  • 6,505
  • 16
  • 19
0

I'm surprised nobody else appears to have solved this on iOS.

I devised a solution that handles both Tab and Shift+Tab to go forward and backward to any field you want on iOS, and doesn't use any private APIs.

I wrote about it here: http://weaklyreferenced.wordpress.com/2012/11/13/responding-to-the-tab-and-shift-tab-keys-on-ios-5-ios-6-with-an-external-keyboard/

AlleyGator
  • 1,266
  • 9
  • 13
0

Might want to print the key received instead of checking for \t to make sure your keyboard handling routine works, and it will tell you what to look for also.

gmlime
  • 1,017
  • 8
  • 17