iam*_*ile 6 iphone iphone-softkeyboard
反正在我的应用程序中捕获所有键盘事件?我需要知道用户是否在我的应用程序中使用键盘输入任何内容(应用程序有多个视图).我能够通过子类化UIWindow捕获touchEvents但无法捕获键盘事件.
oxi*_*gen 13
使用NSNotificationCenter
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil];
........
-(void) keyPressed: (NSNotification*) notification
{
NSLog([[notification object]text]);
}
Run Code Online (Sandbox Code Playgroud)
nac*_*o4d 10
我在博客中写了一篇关于使用UIEvent的小黑客来捕捉事件的文章
有关详细信息,请参阅: iOS中的捕捉键盘事件.
从上面提到的博客:
诀窍在于直接访问GSEventKey结构内存并检查某些字节以了解按下的键的键代码和标志.下面的代码几乎是自解释的,应该放在你的UIApplication子类中.
#define GSEVENT_TYPE 2
#define GSEVENT_FLAGS 12
#define GSEVENTKEY_KEYCODE 15
#define GSEVENT_TYPE_KEYUP 11
NSString *const GSEventKeyUpNotification = @"GSEventKeyUpHackNotification";
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
if ([event respondsToSelector:@selector(_gsEvent)]) {
// Key events come in form of UIInternalEvents.
// They contain a GSEvent object which contains
// a GSEventRecord among other things
int *eventMem;
eventMem = (int *)[event performSelector:@selector(_gsEvent)];
if (eventMem) {
// So far we got a GSEvent :)
int eventType = eventMem[GSEVENT_TYPE];
if (eventType == GSEVENT_TYPE_KEYUP) {
// Now we got a GSEventKey!
// Read flags from GSEvent
int eventFlags = eventMem[GSEVENT_FLAGS];
if (eventFlags) {
// This example post notifications only when
// pressed key has Shift, Ctrl, Cmd or Alt flags
// Read keycode from GSEventKey
int tmp = eventMem[GSEVENTKEY_KEYCODE];
UniChar *keycode = (UniChar *)&tmp;
// Post notification
NSDictionary *inf;
inf = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithShort:keycode[0]],
@"keycode",
[NSNumber numberWithInt:eventFlags],
@"eventFlags",
nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:GSEventKeyUpNotification
object:nil
userInfo:userInfo];
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13706 次 |
| 最近记录: |