Last active
September 17, 2025 04:02
-
-
Save myell0w/d8dfabde43f8da543f9c to your computer and use it in GitHub Desktop.
Revisions
-
myell0w revised this gist
Jan 29, 2015 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // direct check for external keyboard + (BOOL)_isExternalKeyboardAttached { BOOL externalKeyboardAttached = NO; @@ -31,4 +32,18 @@ - (BOOL)_isExternalKeyboardAttached } return externalKeyboardAttached; } // observe change of external keyboard + (void)_registerForExternalKeyboardChangeNotification { CFNotificationCenterRef notificationCenter = CFNotificationCenterGetDarwinNotifyCenter(); if (notificationCenter != NULL) { CFNotificationCenterAddObserver(notificationCenter, NULL, MNExternalKeyboardStatusDidChange, CFSTR("GSEventHardwareKeyboardAttached"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); } } static void MNExternalKeyboardStatusDidChange(CFNotificationCenterRef notificationCenter, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { // external keyboard state changed… } -
myell0w created this gist
Jan 29, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ - (BOOL)_isExternalKeyboardAttached { BOOL externalKeyboardAttached = NO; @try { NSString *keyboardClassName = [@[@"UI", @"Key", @"boa", @"rd", @"Im", @"pl"] componentsJoinedByString:@""]; Class c = NSClassFromString(keyboardClassName); SEL sharedInstanceSEL = NSSelectorFromString(@"sharedInstance"); if (c == Nil || ![c respondsToSelector:sharedInstanceSEL]) { return NO; } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" id sharedKeyboardInstance = [c performSelector:sharedInstanceSEL]; #pragma clang diagnostic pop if (![sharedKeyboardInstance isKindOfClass:NSClassFromString(keyboardClassName)]) { return NO; } NSString *externalKeyboardSelectorName = [@[@"is", @"InH", @"ardw", @"areK", @"eyb", @"oard", @"Mode"] componentsJoinedByString:@""]; SEL externalKeyboardSEL = NSSelectorFromString(externalKeyboardSelectorName); if (![sharedKeyboardInstance respondsToSelector:externalKeyboardSEL]) { return NO; } externalKeyboardAttached = ((BOOL ( *)(id, SEL))objc_msgSend)(sharedKeyboardInstance, externalKeyboardSEL); } @catch(__unused NSException *ex) { externalKeyboardAttached = NO; } return externalKeyboardAttached; }