在iPhone模拟器上按物理键盘时接收通知的简单方法。这对于调试或直接从键盘与界面和应用程序逻辑交互很有用。
基于以下想法:https://github.com/cloudkite/Commando
JRSwizzle - https://github.com/rentzsch/jrswizzle
将PhysicalKeyboardNotifications
目录中的所有文件包括在内
在您的.pch
中添加文件PhysicalKeyboardNotifications.h
将作为观察者添加PKNKeyUpNotification或PKNKeyDownNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardKeyDownNotification:) name:PKNKeyDownNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardKeyDownNotification:) name:PKNKeyUpNotification object:nil];
- (void)onKeyboardKeyUpNotification:(NSNotification*)notification { PKNKeyInputCode keyCode = [notification.userInfo[PKNKeyInputCodeNotificationUserInfoKey] shortValue]; NSString *keyChar = notification.userInfo[PKNKeyInputCharNotificationUserInfoKey]; NSLog(@"KEY UP: %hd, CHAR: %@", keyCode, keyChar); }
PKNKeyInputCharNotificationUserInfoKey
键中,但别担心,PkNDefines.h
中定义了几个宏以便比较它们的keyCodes