2011年11月7日 星期一

解析input context與keyDown

首先,它是NSControl的子類別,而非NSTextView的子類別。

不過它實作了NSTextInput protocol。


這樣它還能適用Cocoa Text Editing的規則嗎?

回顧Cocoa Text Editing流程Overview of Text Editing,當中的圖片顯示,
NSTextView會打一個handleEvent:給NSTextInputContext。
Text-input key event processing

那麼,非NSTextView的QSSearchObjectView,應該宣告一個自己的NSTextInputContext property嗎?
經過一番努力,發現每個NSView已經擁有自己的NSTextInputContext

補充一下發現的過程。在查找NSTextInputContext的class reference時,發現它的property "client"的說明
Discussion
The client (owner) of the input context, typically an NSView instance, retains its NSTextInputContext instance. TheNSTextInputContext instance doesn't retain its client. 
當中提到NSView會maintain一個NSTextInputContext。
從這裡我發現了keyDown的正確改寫方法:

- (void)keyDown:(NSEvent *)theEvent {
    // add your code here   
    [[self inputContext] handleEvent:theEvent];  
}
做完你想做的處理,還是要把event交給input context處理。
不過,一如之前所說,不應該去改keyDown。

沒有留言:

張貼留言