[gtk/gtk-3-24: 1/2] IMContextSimple: Fix AltGr not working on Win32




commit f03bf556885fefa3cc7164d3cde905390b399ee9
Author: Philip Zander <philip zander gmail com>
Date:   Thu Jan 13 13:43:23 2022 +0100

    IMContextSimple: Fix AltGr not working on Win32
    
    The old code assumed that any key press containing Ctrl or Alt cannot be
    regular text input. This is not correct on Win32 as AltGr = Ctrl + Alt.

 gtk/gtkimcontextsimple.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkimcontextsimple.c b/gtk/gtkimcontextsimple.c
index a7ac95e47c..a788426ceb 100644
--- a/gtk/gtkimcontextsimple.c
+++ b/gtk/gtkimcontextsimple.c
@@ -686,6 +686,7 @@ gtk_im_context_simple_filter_keypress (GtkIMContext *context,
   GtkIMContextSimple *context_simple = GTK_IM_CONTEXT_SIMPLE (context);
   GtkIMContextSimplePrivate *priv = context_simple->priv;
   GdkDisplay *display = gdk_window_get_display (event->window);
+  GdkKeymap *keymap = gdk_keymap_get_for_display (display);
   GSList *tmp_list;
   int n_compose = 0;
   GdkModifierType hex_mod_mask;
@@ -787,11 +788,28 @@ gtk_im_context_simple_filter_keypress (GtkIMContext *context,
        !is_hex_start && !is_hex_end && !is_escape && !is_backspace))
     {
       GdkModifierType no_text_input_mask;
+      GdkModifierType consumed_modifiers = 0;
 
       no_text_input_mask =
-        gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
+        gdk_keymap_get_modifier_mask (keymap,
                                       GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
 
+#ifdef G_OS_WIN32
+     /* On Win32, even Ctrl + Alt could be text input because AltGr = Ctrl
+      * + Alt. For example, Ctrl + Alt + e = € on a German keyboard. The
+      * GdkEvent's state, however, reports *all* modifiers that were
+      * active at the time the key was pressed, including the ones that
+      * were consumed to generate the keyval. So we cannot just assume
+      * that any key event containing Ctrl or Alt is a keybinding. We have
+      * to first check if those modifiers were actually used to generate
+      * the keyval. If so, then the keypress is regular input and we
+      * should not exit here.
+      */
+      gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
+                                           event->state, event->group, NULL,
+                                           NULL, NULL, &consumed_modifiers);
+#endif
+
       if (priv->in_hex_sequence && priv->modifiers_dropped &&
          (event->keyval == GDK_KEY_Return ||
           event->keyval == GDK_KEY_ISO_Enter ||
@@ -800,7 +818,7 @@ gtk_im_context_simple_filter_keypress (GtkIMContext *context,
          return FALSE;
        }
 
-      if (event->state & no_text_input_mask)
+      if (event->state & no_text_input_mask & ~consumed_modifiers)
         {
           if (priv->in_hex_sequence || priv->in_compose_sequence)
             return TRUE; /* Don't leak random key events during preedit */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]