PhysicalKeyboardNotifications 0.1

PhysicalKeyboardNotifications 0.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.



  • By
  • brovador

An easy way to receive notifications on iPhone simulator when physical keyboard is pressed. Useful for debugging or playing with the interface and application logic directly from the keyboard.

Based on the idea from: https://github.com/cloudkite/Commando

Dependencies

JRSwizzle - https://github.com/rentzsch/jrswizzle

Usage

  • Include all the files inside the directory PhysicalKeyboardNotifications

  • Add to your .pch the file PhysicalKeyboardNotifications.h

  • Add as observer for PKNKeyUpNotification or PKNKeyDownNotification

[[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(onKeyboardKeyDownNotification:)  
                                                     name:PKNKeyDownNotification  
                                                   object:nil];  
                                                   
[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onKeyboardKeyDownNotification:)
                                                     name:PKNKeyUpNotification
                                                   object:nil];
  • In the notification observer method recover from userInfo dictionary the pressed keyValue or a NSString representing the char
- (void)onKeyboardKeyUpNotification:(NSNotification*)notification
{
    PKNKeyInputCode keyCode = [notification.userInfo[PKNKeyInputCodeNotificationUserInfoKey] shortValue];
    NSString *keyChar = notification.userInfo[PKNKeyInputCharNotificationUserInfoKey];
    NSLog(@"KEY UP: %hd, CHAR: %@", keyCode, keyChar);
}
  • Special keys as space, return, delete or arrows will return and empty string in the key PKNKeyInputCharNotificationUserInfoKey but don't panic there are several macro defined values to compare their keyCodes in PkNDefines.h