是否有無法將所有鍵盤事件鎖定在我的應用程序中?我需要知道用戶是否在我的應用程序中使用鍵盤輸入任何內容(應用程序有多個視圖)。我能夠通過繼承UIWindow捕獲touchEvents,但無法捕獲鍵盤事件。接收iPhone鍵盤事件
回答
使用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]);
}
不是一個簡單的答案,但我認爲你有兩種方法可用。
子類的輸入組件(UITextView的,的UITextField等),因爲你已經與一個UIWindow完成。
創建一個應用程序範圍的UITextViewDelegate(和UITextFieldDelegate)並將所有輸入字段委託分配給它。
我寫了一篇關於使用醒目的UIEvent的小黑客事件在我的博客
請參考: Catching Keyboard Events in 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];
}
}
}
}
}
請參閱[「我怎麼能寫我的答案,正確地鏈接到外部資源?」](http://meta.stackexchange.com/questions/94022/how-could -i-寫我的回答,即鏈接到一個外部的資源,正確)。 – 2012-01-14 17:05:23
我不明白我得到了-1。是的,鏈接是我的博客的一篇文章,但它完全回答了這個問題。它捕獲所有鍵盤事件,包括Shift,Cmd,Ctrl,Alt。如果是因爲使用私有API,我明確表示它是**黑客**。我不是在這裏欺騙任何人。 – nacho4d 2012-01-15 14:49:05
可能因爲你的回答沒有回答這個問題,它只是指向一個可以消失,破碎或者簡單地改變超出這個問題的外部來源。正如安娜李爾指出的那樣,閱讀她指出的部分。下面的答案是遵循的正確規則: - 你解釋鏈接項目的內容(可能省略細節或例子) - 你確定作者(你自己,MSDN等) - 某人可以從中受益在沒有閱讀鏈接項目的情況下回答 - 如果點擊鏈接是值得的,可以包含讓讀者決定的信息 – Coyote 2012-09-04 08:38:43
- 1. iphone鍵盤觸摸事件
- 2. iphone鍵盤keydown/keyup事件?
- 3. 爲什麼我的鍵盤掛鉤多次接收到相同的鍵盤鍵和鍵盤鍵事件?
- 4. Sprite沒有收到鍵盤事件
- 5. 鍵盤事件
- 6. 鍵盤事件
- 7. 鍵盤事件
- 8. 安卓與SDL2未接收數字/字母的鍵盤事件
- 9. 如何接收未聚焦的QtQuick Item中的鍵盤事件?
- 10. PyQt:如何接收子類QWidget中的鍵盤事件?
- 11. Godot鍵盤事件
- 12. GLUT鍵盤事件
- 13. Flex鍵盤事件
- 14. As3鍵盤事件
- 15. 如何檢測iPhone上的硬件鍵盤上的鍵盤事件(iOS)
- 16. WPF 4鍵盤焦點:從來沒有收到關鍵事件
- 17. 觸發鍵盤事件,WebKit的鍵盤不會觸發事件
- 18. 硬件鍵盤事件
- 19. 鍵盤iphone
- 20. android軟鍵盤事件
- 21. jQuery鍵盤延遲事件
- 22. 綁定javascript鍵盤事件
- 23. 高級Python鍵盤事件?
- 24. Actionscript 3 Flash鍵盤事件
- 25. UICatalog和鍵盤事件
- 26. 模擬鍵盤事件
- 27. 鍵盤事件教程C#
- 28. 發佈鍵盤事件
- 29. .NET中的鍵盤事件
- 30. Javascript幾個鍵盤事件
這現在有點棘手,涉及很多工作。我希望看到一些應用程序級別的事件捕獲 – iamMobile 2009-08-12 18:46:42