cmd + period(.) does not work as expected
I develop an application on Mac using cocoa. I need to handle cmd + period(.) keyboard event as a command I designed. But now the cmd + period(.) keyboard event does not well handled as I expected.
In the cocoa keyEvent handle process, if the application object processes a key event and it turns out not to be a key equivalent or a key interface control event, it then sends it to the key window in a sendEvent: message. The window object 开发者_开发百科invokes the keyDown: method in the first responder. My handle for cmd + period(.) is in keyDown: method.
But the problem is that Mac treates cmd + period(.) key the same as Escape key. The key window first searches the view hierarchy for a view whose key equivalent is Escape or Command-., whichever was entered. But none of these views handles the key equivalent, then a cancel: action message is sent to the first responder in the responder chain. So cmd + period(.) is handled as a cancel operation before it reaches keyDown: method.
Can anyone have some idea to solve this problem. And make the cmd + period(.) be handled as I expected but as cancel command. Thank you.
What is more, it is better not handle the cmd + period(.) when do the key equivalent check(performkeyEquivalent).
If you want to override the default handling, you need to catch the keyboard event earlier in the chain. For example, subclass NSWindow and override -sendEvent, or even more thorough, subclass NSApplication and override -nextEventMatchingMask (all events will pass through this function).
精彩评论