Created
August 2, 2011 08:33
-
-
Save stuartcarnie/1119822 to your computer and use it in GitHub Desktop.
Code to detect when hardware keyboard is attached / detached
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 characters
#include <objc/runtime.h> | |
// this is called when a keyboard is attached / detached, and | |
// isHardwareAttached will return YES / NO appropriately | |
void hardwareChangeIMP(id aSelf, SEL sel, BOOL isHardwareAttached) { | |
SEL newSel = NSSelectorFromString(@"_modeChange:"); | |
[aSelf performSelector:newSel withObject:[NSNumber numberWithBool:isHardwareAttached]]; | |
} | |
// call this to start capturing hardware changes | |
void captureKeyboardHardwareChanges() { | |
Class kb = NSClassFromString(@"UIKeyboardImpl"); | |
SEL origSel = NSSelectorFromString(@"setInHardwareKeyboardMode:"); | |
SEL newSel = NSSelectorFromString(@"_modeChange:"); | |
class_addMethod(kb, newSel, (IMP)hardwareChangeIMP, @encode(BOOL)); | |
Method orig = class_getInstanceMethod(kb, origSel); | |
Method alt = class_getInstanceMethod(kb, newSel); | |
method_exchangeImplementations(orig, alt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment