Skip to content

Instantly share code, notes, and snippets.

@stuartcarnie
Created August 2, 2011 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartcarnie/1119822 to your computer and use it in GitHub Desktop.
Save stuartcarnie/1119822 to your computer and use it in GitHub Desktop.
Code to detect when hardware keyboard is attached / detached
#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