Skip to content

Gtk.IMContext

class — extends GObject.Object

The interface for GTK input methods.

GtkIMContext is used by GTK text input widgets like GtkText to map from key events to Unicode character strings.

An input method may consume multiple key events in sequence before finally outputting the composed result. This is called preediting, and an input method may provide feedback about this process by displaying the intermediate composition states as preedit text. To do so, the GtkIMContext will emit IMContext.preedit-start, IMContext.preedit-changed and IMContext.preedit-end signals.

For instance, the built-in GTK input method IMContextSimple implements the input of arbitrary Unicode code points by holding down the <kbd>Control</kbd> and <kbd>Shift</kbd> keys and then typing <kbd>u</kbd> followed by the hexadecimal digits of the code point. When releasing the <kbd>Control</kbd> and <kbd>Shift</kbd> keys, preediting ends and the character is inserted as text. For example,

Ctrl+Shift+u 2 0 A C

results in the € sign.

Additional input methods can be made available for use by GTK widgets as loadable modules. An input method module is a small shared library which provides a GIOExtension for the extension point named "gtk-im-module".

To connect a widget to the users preferred input method, you should use IMMulticontext.

Methods

activate_osk

def activate_osk(self, event: Gdk.Event | None = ...) -> bool

Requests the platform to show an on-screen keyboard for user input.

This method will return True if this request was actually performed to the platform, other environmental factors may result in an on-screen keyboard effectively not showing up.

Parameters:

delete_surrounding

def delete_surrounding(self, offset: int, n_chars: int) -> bool

Asks the widget that the input context is attached to delete characters around the cursor position by emitting the ::delete_surrounding signal.

Note that offset and n_chars are in characters not in bytes which differs from the usage other places in GtkIMContext.

In order to use this function, you should first call IMContext.get_surrounding to get the current context, and call this function immediately afterwards to make sure that you know what you are deleting. You should also account for the fact that even if the signal was handled, the input context might not have deleted all the characters that were requested to be deleted.

This function is used by an input method that wants to make substitutions in the existing text in response to new input. It is not useful for applications.

Parameters:

  • offset — offset from cursor position in chars; a negative value means start before the cursor.
  • n_chars — number of characters to delete.

filter_key

def filter_key(self, press: bool, surface: Gdk.Surface, device: Gdk.Device, time: int, keycode: int, state: Gdk.ModifierType | int, group: int) -> bool

Allow an input method to forward key press and release events to another input method without necessarily having a GdkEvent available.

Parameters:

  • press — whether to forward a key press or release event
  • surface — the surface the event is for
  • device — the device that the event is for
  • time — the timestamp for the event
  • keycode — the keycode for the event
  • state — modifier state for the event
  • group — the active keyboard group for the event

filter_keypress

def filter_keypress(self, event: Gdk.Event) -> bool

Allow an input method to internally handle key press and release events.

If this function returns True, then no further processing should be done for this key event.

Parameters:

  • event — the key event

focus_in

def focus_in(self) -> None

Notify the input method that the widget to which this input context corresponds has gained focus.

The input method may, for example, change the displayed feedback to reflect this change.

focus_out

def focus_out(self) -> None

Notify the input method that the widget to which this input context corresponds has lost focus.

The input method may, for example, change the displayed feedback or reset the contexts state to reflect this change.

get_preedit_string

def get_preedit_string(self) -> tuple[str, Pango.AttrList, int]

Retrieve the current preedit string for the input context, and a list of attributes to apply to the string.

This string should be displayed inserted at the insertion point.

get_surrounding

def get_surrounding(self) -> tuple[bool, str, int]

:::warning Deprecated since 4.2 This API is deprecated. :::

Retrieves context around the insertion point.

Input methods typically want context in order to constrain input text based on existing text; this is important for languages such as Thai where only some sequences of characters are allowed.

This function is implemented by emitting the IMContext.retrieve-surrounding signal on the input method; in response to this signal, a widget should provide as much context as is available, up to an entire paragraph, by calling IMContext.set_surrounding.

Note that there is no obligation for a widget to respond to the ::retrieve-surrounding signal, so input methods must be prepared to function without context.

get_surrounding_with_selection

def get_surrounding_with_selection(self) -> tuple[bool, str, int, int]

Retrieves context around the insertion point.

Input methods typically want context in order to constrain input text based on existing text; this is important for languages such as Thai where only some sequences of characters are allowed.

This function is implemented by emitting the IMContext.retrieve-surrounding signal on the input method; in response to this signal, a widget should provide as much context as is available, up to an entire paragraph, by calling IMContext.set_surrounding_with_selection.

Note that there is no obligation for a widget to respond to the ::retrieve-surrounding signal, so input methods must be prepared to function without context.

reset

def reset(self) -> None

Notify the input method that a change such as a change in cursor position has been made.

This will typically cause the input method to clear the preedit state.

set_client_widget

def set_client_widget(self, widget: Widget | None = ...) -> None

Set the client widget for the input context.

This is the GtkWidget holding the input focus. This widget is used in order to correctly position status windows, and may also be used for purposes internal to the input method.

Parameters:

  • widget — the client widget. This may be None to indicate that the previous client widget no longer exists.

set_cursor_location

def set_cursor_location(self, area: Gdk.Rectangle) -> None

Notify the input method that a change in cursor position has been made.

The location is relative to the client widget.

Parameters:

  • area — new location

set_surrounding

def set_surrounding(self, text: str, len: int, cursor_index: int) -> None

:::warning Deprecated since 4.2 This API is deprecated. :::

Sets surrounding context around the insertion point and preedit string.

This function is expected to be called in response to the IMContext.retrieve-surrounding signal, and will likely have no effect if called at other times.

Parameters:

  • text — text surrounding the insertion point, as UTF-8. the preedit string should not be included within text
  • len — the length of text, or -1 if text is nul-terminated
  • cursor_index — the byte index of the insertion cursor within text.

set_surrounding_with_selection

def set_surrounding_with_selection(self, text: str, len: int, cursor_index: int, anchor_index: int) -> None

Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the IMContext.retrieve_surrounding signal, and will likely have no effect if called at other times.

Parameters:

  • text — text surrounding the insertion point, as UTF-8. the preedit string should not be included within text
  • len — the length of text, or -1 if text is nul-terminated
  • cursor_index — the byte index of the insertion cursor within text
  • anchor_index — the byte index of the selection bound within text

set_use_preedit

def set_use_preedit(self, use_preedit: bool) -> None

Sets whether the IM context should use the preedit string to display feedback.

If use_preedit is False (default is True), then the IM context may use some other method to display feedback, such as displaying it in a child of the root window.

Parameters:

  • use_preedit — whether the IM context should use the preedit string.

Virtual methods

do_activate_osk

def do_activate_osk(self) -> None

do_activate_osk_with_event

def do_activate_osk_with_event(self, event: Gdk.Event) -> bool

do_commit

def do_commit(self, str: str) -> None

Default handler of the IMContext.commit signal.

do_delete_surrounding

def do_delete_surrounding(self, offset: int, n_chars: int) -> bool

Asks the widget that the input context is attached to delete characters around the cursor position by emitting the ::delete_surrounding signal.

Note that offset and n_chars are in characters not in bytes which differs from the usage other places in GtkIMContext.

In order to use this function, you should first call IMContext.get_surrounding to get the current context, and call this function immediately afterwards to make sure that you know what you are deleting. You should also account for the fact that even if the signal was handled, the input context might not have deleted all the characters that were requested to be deleted.

This function is used by an input method that wants to make substitutions in the existing text in response to new input. It is not useful for applications.

Parameters:

  • offset — offset from cursor position in chars; a negative value means start before the cursor.
  • n_chars — number of characters to delete.

do_filter_keypress

def do_filter_keypress(self, event: Gdk.Event) -> bool

Allow an input method to internally handle key press and release events.

If this function returns True, then no further processing should be done for this key event.

Parameters:

  • event — the key event

do_focus_in

def do_focus_in(self) -> None

Notify the input method that the widget to which this input context corresponds has gained focus.

The input method may, for example, change the displayed feedback to reflect this change.

do_focus_out

def do_focus_out(self) -> None

Notify the input method that the widget to which this input context corresponds has lost focus.

The input method may, for example, change the displayed feedback or reset the contexts state to reflect this change.

do_get_preedit_string

def do_get_preedit_string(self) -> tuple[str, Pango.AttrList, int]

Retrieve the current preedit string for the input context, and a list of attributes to apply to the string.

This string should be displayed inserted at the insertion point.

do_get_surrounding

def do_get_surrounding(self) -> tuple[bool, str, int]

:::warning Deprecated since 4.2 This API is deprecated. :::

Retrieves context around the insertion point.

Input methods typically want context in order to constrain input text based on existing text; this is important for languages such as Thai where only some sequences of characters are allowed.

This function is implemented by emitting the IMContext.retrieve-surrounding signal on the input method; in response to this signal, a widget should provide as much context as is available, up to an entire paragraph, by calling IMContext.set_surrounding.

Note that there is no obligation for a widget to respond to the ::retrieve-surrounding signal, so input methods must be prepared to function without context.

do_get_surrounding_with_selection

def do_get_surrounding_with_selection(self) -> tuple[bool, str, int, int]

Retrieves context around the insertion point.

Input methods typically want context in order to constrain input text based on existing text; this is important for languages such as Thai where only some sequences of characters are allowed.

This function is implemented by emitting the IMContext.retrieve-surrounding signal on the input method; in response to this signal, a widget should provide as much context as is available, up to an entire paragraph, by calling IMContext.set_surrounding_with_selection.

Note that there is no obligation for a widget to respond to the ::retrieve-surrounding signal, so input methods must be prepared to function without context.

do_invalid_composition

def do_invalid_composition(self, str: str) -> bool

Default handler of the IMContext.invalid-composition signal. Since: 4.22

do_preedit_changed

def do_preedit_changed(self) -> None

Default handler of the IMContext.preedit-changed signal.

do_preedit_end

def do_preedit_end(self) -> None

Default handler of the IMContext.preedit-end signal.

do_preedit_start

def do_preedit_start(self) -> None

Default handler of the IMContext.preedit-start signal.

do_reset

def do_reset(self) -> None

Notify the input method that a change such as a change in cursor position has been made.

This will typically cause the input method to clear the preedit state.

do_retrieve_surrounding

def do_retrieve_surrounding(self) -> bool

Default handler of the IMContext.retrieve-surrounding signal.

do_set_client_widget

def do_set_client_widget(self, widget: Widget | None = ...) -> None

Set the client widget for the input context.

This is the GtkWidget holding the input focus. This widget is used in order to correctly position status windows, and may also be used for purposes internal to the input method.

Parameters:

  • widget — the client widget. This may be None to indicate that the previous client widget no longer exists.

do_set_cursor_location

def do_set_cursor_location(self, area: Gdk.Rectangle) -> None

Notify the input method that a change in cursor position has been made.

The location is relative to the client widget.

Parameters:

  • area — new location

do_set_surrounding

def do_set_surrounding(self, text: str, len: int, cursor_index: int) -> None

:::warning Deprecated since 4.2 This API is deprecated. :::

Sets surrounding context around the insertion point and preedit string.

This function is expected to be called in response to the IMContext.retrieve-surrounding signal, and will likely have no effect if called at other times.

Parameters:

  • text — text surrounding the insertion point, as UTF-8. the preedit string should not be included within text
  • len — the length of text, or -1 if text is nul-terminated
  • cursor_index — the byte index of the insertion cursor within text.

do_set_surrounding_with_selection

def do_set_surrounding_with_selection(self, text: str, len: int, cursor_index: int, anchor_index: int) -> None

Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the IMContext.retrieve_surrounding signal, and will likely have no effect if called at other times.

Parameters:

  • text — text surrounding the insertion point, as UTF-8. the preedit string should not be included within text
  • len — the length of text, or -1 if text is nul-terminated
  • cursor_index — the byte index of the insertion cursor within text
  • anchor_index — the byte index of the selection bound within text

do_set_use_preedit

def do_set_use_preedit(self, use_preedit: bool) -> None

Sets whether the IM context should use the preedit string to display feedback.

If use_preedit is False (default is True), then the IM context may use some other method to display feedback, such as displaying it in a child of the root window.

Parameters:

  • use_preedit — whether the IM context should use the preedit string.

Properties

input_hints

input_hints: InputHints | int  # read/write

Additional hints that allow input methods to fine-tune their behaviour.

input_purpose

input_purpose: InputPurpose | int  # read/write

The purpose of the text field that the `GtkIMContext is connected to.

This property can be used by on-screen keyboards and other input methods to adjust their behaviour.

Signals

commit

def on_commit(self, str: str) -> None: ...

The ::commit signal is emitted when a complete input sequence has been entered by the user.

If the commit comes after a preediting sequence, the ::commit signal is emitted after ::preedit-end.

This can be a single character immediately after a key press or the final result of preediting.

delete-surrounding

def on_delete_surrounding(self, offset: int, n_chars: int) -> bool: ...

The ::delete-surrounding signal is emitted when the input method needs to delete all or part of the context surrounding the cursor.

invalid-composition

def on_invalid_composition(self, str: str) -> bool: ...

Emitted when the filtered keys do not compose to a single valid character.

preedit-changed

def on_preedit_changed(self) -> None: ...

The ::preedit-changed signal is emitted whenever the preedit sequence currently being entered has changed.

It is also emitted at the end of a preedit sequence, in which case IMContext.get_preedit_string returns the empty string.

preedit-end

def on_preedit_end(self) -> None: ...

The ::preedit-end signal is emitted when a preediting sequence has been completed or canceled.

preedit-start

def on_preedit_start(self) -> None: ...

The ::preedit-start signal is emitted when a new preediting sequence starts.

retrieve-surrounding

def on_retrieve_surrounding(self) -> bool: ...

The ::retrieve-surrounding signal is emitted when the input method requires the context surrounding the cursor.

The callback should set the input method surrounding context by calling the IMContext.set_surrounding method.