:right-sidebar: True IMContext =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: IMContext(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` Subclasses: :class:`~gi.repository.Gtk.IMContextSimple`, :class:`~gi.repository.Gtk.IMMulticontext` ``GtkIMContext`` defines 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 :obj:`~gi.repository.Gtk.IMContext.signals.preedit_start`, :obj:`~gi.repository.Gtk.IMContext.signals.preedit_changed` and :obj:`~gi.repository.Gtk.IMContext.signals.preedit_end` signals. For instance, the built-in GTK input method :obj:`~gi.repository.Gtk.IMContextSimple` implements the input of arbitrary Unicode code points by holding down the :kbd:`Control` and :kbd:`Shift` keys and then typing :kbd:`u` followed by the hexadecimal digits of the code point. When releasing the :kbd:`Control` and :kbd:`Shift` 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 :obj:`~gi.repository.Gtk.IMMulticontext`. Methods ------- .. rst-class:: interim-class .. class:: IMContext :no-index: .. method:: activate_osk(event: ~gi.repository.Gdk.Event | None = None) -> bool Requests the platform to show an on-screen keyboard for user input. This method will return :const:`True` if this request was actually performed to the platform, other environmental factors may result in an on-screen keyboard effectively not showing up. .. versionadded:: 4.14 :param event: a :obj:`~gi.repository.Gdk.Event` .. method:: delete_surrounding(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 :obj:`~gi.repository.Gtk.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. :param offset: offset from cursor position in chars; a negative value means start before the cursor. :param n_chars: number of characters to delete. .. method:: filter_key(press: bool, surface: ~gi.repository.Gdk.Surface, device: ~gi.repository.Gdk.Device, time: int, keycode: int, state: ~gi.repository.Gdk.ModifierType, group: int) -> bool Allow an input method to forward key press and release events to another input method without necessarily having a ``GdkEvent`` available. :param press: whether to forward a key press or release event :param surface: the surface the event is for :param device: the device that the event is for :param time: the timestamp for the event :param keycode: the keycode for the event :param state: modifier state for the event :param group: the active keyboard group for the event .. method:: filter_keypress(event: ~gi.repository.Gdk.Event) -> bool Allow an input method to internally handle key press and release events. If this function returns :const:`True`, then no further processing should be done for this key event. :param event: the key event .. method:: focus_in() -> 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. .. method:: focus_out() -> 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. .. method:: get_preedit_string() -> tuple[str, ~gi.repository.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. .. method:: get_surrounding() -> tuple[bool, str, 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 :obj:`~gi.repository.Gtk.IMContext.signals.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 :obj:`~gi.repository.Gtk.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. .. deprecated:: 4.2 Use :obj:`~gi.repository.Gtk.IMContext.get_surrounding_with_selection` instead. .. method:: get_surrounding_with_selection() -> 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 :obj:`~gi.repository.Gtk.IMContext.signals.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 :obj:`~gi.repository.Gtk.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. .. versionadded:: 4.2 .. method:: reset() -> 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. .. method:: set_client_widget(widget: ~gi.repository.Gtk.Widget | None = 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. :param widget: the client widget. This may be :const:`None` to indicate that the previous client widget no longer exists. .. method:: set_cursor_location(area: ~gi.repository.Gdk.Rectangle) -> None Notify the input method that a change in cursor position has been made. The location is relative to the client widget. :param area: new location .. method:: set_surrounding(text: str, len: int, cursor_index: int) -> None Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the :obj:`~gi.repository.Gtk.IMContext.signals.retrieve_surrounding` signal, and will likely have no effect if called at other times. .. deprecated:: 4.2 Use :obj:`~gi.repository.Gtk.IMContext.set_surrounding_with_selection` instead :param text: text surrounding the insertion point, as UTF-8. the preedit string should not be included within ``text`` :param len: the length of ``text``, or -1 if ``text`` is nul-terminated :param cursor_index: the byte index of the insertion cursor within ``text``. .. method:: set_surrounding_with_selection(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 :obj:`~gi.repository.Gtk.IMContext.signals.retrieve_surrounding` signal, and will likely have no effect if called at other times. .. versionadded:: 4.2 :param text: text surrounding the insertion point, as UTF-8. the preedit string should not be included within ``text`` :param len: the length of ``text``, or -1 if ``text`` is nul-terminated :param cursor_index: the byte index of the insertion cursor within ``text`` :param anchor_index: the byte index of the selection bound within ``text`` .. method:: set_use_preedit(use_preedit: bool) -> None Sets whether the IM context should use the preedit string to display feedback. If ``use_preedit`` is :const:`False` (default is :const:`True`), then the IM context may use some other method to display feedback, such as displaying it in a child of the root window. :param use_preedit: whether the IM context should use the preedit string. Properties ---------- .. rst-class:: interim-class .. class:: IMContext :no-index: .. attribute:: props.input_hints :type: ~gi.repository.Gtk.InputHints Additional hints that allow input methods to fine-tune their behaviour. .. attribute:: props.input_purpose :type: ~gi.repository.Gtk.InputPurpose 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 ------- .. rst-class:: interim-class .. class:: IMContext.signals :no-index: .. method:: commit(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. :param str: the completed character(s) entered by the user .. method:: delete_surrounding(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. :param offset: the character offset from the cursor position of the text to be deleted. A negative value indicates a position before the cursor. :param n_chars: the number of characters to be deleted .. method:: preedit_changed() -> 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 :obj:`~gi.repository.Gtk.IMContext.get_preedit_string` returns the empty string. .. method:: preedit_end() -> None The ::preedit-end signal is emitted when a preediting sequence has been completed or canceled. .. method:: preedit_start() -> None The ::preedit-start signal is emitted when a new preediting sequence starts. .. method:: retrieve_surrounding() -> 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 :obj:`~gi.repository.Gtk.IMContext.set_surrounding` method. Virtual Methods --------------- .. rst-class:: interim-class .. class:: IMContext :no-index: .. method:: do_activate_osk() -> None .. method:: do_activate_osk_with_event(event: ~gi.repository.Gdk.Event) -> bool :param event: .. method:: do_commit(str: str) -> None Default handler of the :obj:`~gi.repository.Gtk.IMContext.signals.commit` signal. :param str: .. method:: do_delete_surrounding(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 :obj:`~gi.repository.Gtk.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. :param offset: offset from cursor position in chars; a negative value means start before the cursor. :param n_chars: number of characters to delete. .. method:: do_filter_keypress(event: ~gi.repository.Gdk.Event) -> bool Allow an input method to internally handle key press and release events. If this function returns :const:`True`, then no further processing should be done for this key event. :param event: the key event .. method:: do_focus_in() -> 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. .. method:: do_focus_out() -> 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. .. method:: do_get_preedit_string() -> tuple[str, ~gi.repository.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. .. method:: do_get_surrounding() -> tuple[bool, str, 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 :obj:`~gi.repository.Gtk.IMContext.signals.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 :obj:`~gi.repository.Gtk.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. .. deprecated:: 4.2 Use :obj:`~gi.repository.Gtk.IMContext.get_surrounding_with_selection` instead. .. method:: do_get_surrounding_with_selection() -> 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 :obj:`~gi.repository.Gtk.IMContext.signals.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 :obj:`~gi.repository.Gtk.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. .. versionadded:: 4.2 .. method:: do_preedit_changed() -> None Default handler of the :obj:`~gi.repository.Gtk.IMContext.signals.preedit_changed` signal. .. method:: do_preedit_end() -> None Default handler of the :obj:`~gi.repository.Gtk.IMContext.signals.preedit_end` signal. .. method:: do_preedit_start() -> None Default handler of the :obj:`~gi.repository.Gtk.IMContext.signals.preedit_start` signal. .. method:: do_reset() -> 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. .. method:: do_retrieve_surrounding() -> bool Default handler of the :obj:`~gi.repository.Gtk.IMContext.signals.retrieve_surrounding` signal. .. method:: do_set_client_widget(widget: ~gi.repository.Gtk.Widget | None = 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. :param widget: the client widget. This may be :const:`None` to indicate that the previous client widget no longer exists. .. method:: do_set_cursor_location(area: ~gi.repository.Gdk.Rectangle) -> None Notify the input method that a change in cursor position has been made. The location is relative to the client widget. :param area: new location .. method:: do_set_surrounding(text: str, len: int, cursor_index: int) -> None Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the :obj:`~gi.repository.Gtk.IMContext.signals.retrieve_surrounding` signal, and will likely have no effect if called at other times. .. deprecated:: 4.2 Use :obj:`~gi.repository.Gtk.IMContext.set_surrounding_with_selection` instead :param text: text surrounding the insertion point, as UTF-8. the preedit string should not be included within ``text`` :param len: the length of ``text``, or -1 if ``text`` is nul-terminated :param cursor_index: the byte index of the insertion cursor within ``text``. .. method:: do_set_surrounding_with_selection(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 :obj:`~gi.repository.Gtk.IMContext.signals.retrieve_surrounding` signal, and will likely have no effect if called at other times. .. versionadded:: 4.2 :param text: text surrounding the insertion point, as UTF-8. the preedit string should not be included within ``text`` :param len: the length of ``text``, or -1 if ``text`` is nul-terminated :param cursor_index: the byte index of the insertion cursor within ``text`` :param anchor_index: the byte index of the selection bound within ``text`` .. method:: do_set_use_preedit(use_preedit: bool) -> None Sets whether the IM context should use the preedit string to display feedback. If ``use_preedit`` is :const:`False` (default is :const:`True`), then the IM context may use some other method to display feedback, such as displaying it in a child of the root window. :param use_preedit: whether the IM context should use the preedit string. Fields ------ .. rst-class:: interim-class .. class:: IMContext :no-index: .. attribute:: parent_instance