SDL and Text Input in Mac OS X

Hi all,

In this email I will discuss how to add text input support for a
Mac OS X Cocoa application. It will cover the required changes
for SDL to support Unicode text input in Mac OS X.

For a simple Cocoa application which based on widgets like
NSTextField or NSTextView, Unicode text input support is already
fully implemented, however as we can see SDL on Mac OS X is running
as a custom application with a custom view, so we can’t use high
level GUI widgets like NSTextField, in turn, we need to implement
the NSTextInput protocol 1 or NSTextInputClient protocol 2 for
the input element. My friend yllan has a nice introduction to the
NSTextInput protocol 3, I strongly recommend you to read that
before continue reading this email.

At present, SDL will intercept all NSKeyDown, NSKeyUp events in
the customized event loop of NSApp 4 of SDL_cocoaevents.m, it
will let Cocoa_HandleKeyEvent() 5 to handle them, at this point,
SDL will use a “field editor” created on initialization to interpret
this keyboard event then as for interpreted characters, which,
unfortunately is incorrect, because as described in 3, even
though we need to send keys to interpretKeyEvents: of fieldEdit,
we can’t ask it directly for the characters, we need to have a
callback method called insertText: in that fieldEdit, when a new
batch of characters is committed, that method will be called.

So in short, what we need to do is:

  1. add insertText: in fieldEdit
  2. make the fieldEdit the first responder of that window, otherwise
    we cannot receive insertText:

That’s what I did in the gsoc2009_IME branch.

However, it’s not complete support yet, will need to support
"marked text" in fieldEdit too, so that apps using SDL can have
"on the spot" text editing support. That’s what I am implementing.
And this will involves API changing.

Anther trivial issue is, since SDL intercepted all the key events,
system-wide keyboard shortcuts like CMD + Space will not be handled
within the SDL application, so users cannot use keyboard to switch
input methods, which is not so convenient. That’s why I added 6
in SDL_cocoaevents.m, so that system-wide keyboard shortcuts like
CMD + Space, CMD + Shift + Space, etc. can be handled by the system.

  • Jiang