11

Possible Duplicate:
iPad: detect if external keyboard is present

I've been digging around in the reference library and just can't seem to find the answer here.

I'm assuming there's some API somewhere that I can query to find out if an external hardware keyboard is in use or not.

Update I just tried the EAAccessoryManager.connectedAccessories from the ExternalAccessory.framework. That was a no-go, it returns an empty array when the hardware keyboard is enabled.

Community
  • 1
  • 1
user174448
  • 223
  • 3
  • 8
  • Just curious: Why would you need this information? – Eiko Jul 01 '10 at 15:33
  • When the hardware keyboard is enabled the onscreen keyboard no longer shows up. We're adding a datepicker as a subview over the keyboard for a textfield we have that uses a date. This is sort of a hack due to using the Three20 TTMessageController infrastructure that only allows for UITextFields as fields in a message. When the user touches our "date" text field, we find the keyboard and overlay the UIDatePicker instead. – user174448 Jul 01 '10 at 15:54
  • Determing if he needs to account for the virtual keyboards' size when scrolling input for one. – jamone Jul 01 '10 at 15:55
  • user174448 and @jamone- Did you guys ever figure out a solution to your problem where you had a date picker replacing the onscreen keyboard and needed to handle the case/or detect a hardware keyboard? – kris Nov 20 '11 at 22:00
  • actually, what I'm seeing in the simulator is that if you're setting the inputView property of uitextfield, then iOS knows to show the keyboard anyway. Sounds like that would solve your situation. Not sure if this was an option back when this q was originally asked but thought I'd post anyway – kris Nov 20 '11 at 22:52
  • I think this should answer your question: http://stackoverflow.com/questions/2893267/ipad-detect-if-external-keyboard-is-present – Zebs Mar 06 '12 at 21:53
  • See [This SO question & answer](http://stackoverflow.com/questions/2893267/ipad-detect-if-external-keyboard-is-present) for one way to do it. Also, consider voting to close this question as a duplicate, if you agree that it is. – Olie Apr 12 '12 at 19:13

1 Answers1

1

I think you have to use following code -

- (void)viewDidLoad {
    UIView*  _noExternalAccessoriesPosterView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_noExternalAccessoriesPosterView setBackgroundColor:[UIColor whiteColor]];
    _noExternalAccessoriesLabelView = [[UILabel alloc] initWithFrame:CGRectMake(60, 170, 240, 50)];
    [_noExternalAccessoriesLabelView setText:@"No Accessories Connected"];
    [_noExternalAccessoriesPosterView addSubview:_noExternalAccessoriesLabelView];
    [[self view] addSubview:_noExternalAccessoriesPosterView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];

    _eaSessionController = [EADSessionController sharedController];
    _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];

    [self setTitle:@"Accessories"];

    if ([_accessoryList count] == 0) {
        [_noExternalAccessoriesPosterView setHidden:NO];
    } else {
        [_noExternalAccessoriesPosterView setHidden:YES];
    }
}

- (void)_accessoryDidConnect:(NSNotification *)notification {
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
    [_accessoryList addObject:connectedAccessory];

    if ([_accessoryList count] == 0) {
        [_noExternalAccessoriesPosterView setHidden:NO];
    } else {
        [_noExternalAccessoriesPosterView setHidden:YES];
    }

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([_accessoryList count] - 1) inSection:0];
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

Hope this works for you and remember you have to use ExternalAccessory Framework for this code.

chown
  • 51,908
  • 16
  • 134
  • 170
Nishant Mahajan
  • 264
  • 4
  • 9