Murray's Blog

Creating GTK+ input methods

I have just implemented a GTK+ input method to allow input via multiple key presses, with a timeout, as on mobile phones.

(Update: here is a source tarball – improvements welcome. I’m now wondering how I can specify this as the default input method.)

I didn’t find any documentation about how to do this, but there are examples:

From these examples, I figured out the following, which I might try to get into the gtk-doc documentation later:

Functions that the shared library should export

The shared library must export:

Updating gtk.immodules

The input modules must be listed in the /etc/gtk-2.0/gtk.immodules file, which is a cache of input modules information. Your Makefile should generate a new gtk.immodules file, using gtk-query-immodules-2.0, then install it.

Implementing a GtkIMContext

You must implement a GtkIMContext, an instance of which will be returned by your im_module_create() function.

Though most GObjects would have a *_get_type() function that calls g_type_register_static(), input modules need a *_register_type() function that takes a GTypeModule, which it provides to g_type_module_register_type(). This *_register_type() function should be called from your im_module_init() function. This means:

For “table-based” input methods, you can just derive from GtkIMContextSimple and call gtk_im_context_simple_add_table() with your table array, which specifies which sequences of characters can be used to specify an actual output character. When entering one of these intermediate characters an underline indicates that the user is in compose mode. Pressing the right arrow, or pressing a non-relevant character, accepts the displayed character. For instance, see the Tigrigna-Eritrean input method from GTK+, or the Esperanto input method in gtk-im-extra.

GtkIMContext has several virtual functions that you may want to override. These are not documented, but you can at least see their signatures in the GtkIMContext header file. The most important ones seem to be:

Exit mobile version