keydown, NSEvent and Option key combos

Hi.


i'm implementing a custom NSView where i handle keyboard entry via keyDown and processing NSEvents manually. This is for a text editor, and i have my reaosns for not descerifning from any existing NSTextField or the like.


Everything is working fine in general, but there is one thing i cannot figure out, and cannot find any documentation for, no matter how deep i dig (the Key Events docs topic just ignores the matter completely): How to handle multi-keystroke Option key combos (e.g. typing Option-U, A to get an umlaut "Ä"). i of course receive key evebts for both Option-U and A, the first one with an empty "charcaters" string. But i cannot find where to ask the text system how to combine these two (aside from doing it manually by hardcoding each combo i know about, explicitly). Surely this logic must be exposed somewhere in a resuable way, wher ek coiuld ask Cocoa, e.g. "does this even start a multi-key entry" and "whats ther result for these two, combined", and it should not be necessary for me to hardcode/re-implememt the rules for this, myself — right?


Any ideas, suggestions, pointers?


thanx,

marc

Answered by dwarfland in 171577022

If the Option key is still down, you should get the correct "Ä" character from the "characters" property of the "A" key event.


If this behavior doesn't fit in with what you're trying to do, the next-best choice is to invoke 'interpretKeyEvents' from your 'keyDown' override. The result will show up in a call to 'insertText:' or 'doCommandBySelector:'. The latter will use system defaults for translating keystrokes into commands, which is a Really Good Thing™ if you want your text entry to behave like text entry should.


These APIs are described here:

developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/HandlingKeyEvents.html


and you should not try to do anything with NSEvent handling without understanding everything in the document very well.


If none of the above is compatible with what you're trying to, the next fallback is to use the the system's keyboard mapping (UCHR) tables to translate the raw key codes yourself. I think this is documented somewhere, but I always have trouble finding where. Use one of the easier approaches if you possibly can.

Accepted Answer
keydown, NSEvent and Option key combos
 
 
Q