How to disable GtkTextView default shortcuts in GTK4?

Hi,

In fact I want to disable some shortcuts in GtkSourceView5 and rebind some others, in GTK3 era this piece of CSS worked:

/* Unset key bindings */
@binding-set TextViewKeyBindings
{
  unbind "<Alt>Right";     /* move-words */
  unbind "<Alt>Left";      /* move-words */
  unbind "<Alt>Up";        /* move-lines */
  unbind "<Alt>Down";      /* move-lines */
  unbind "<Alt><Shift>Up"; /* move-viewport */
  unbind "<Alt><Shift>Down"; /* move-viewport */

  bind "<Ctrl><Shift>Up"   { "move-lines" (0) };
  bind "<Ctrl><Shift>Down" { "move-lines" (1) };
  bind "<Ctrl>Up"    { "move-viewport" (0, -1) };
  bind "<Ctrl>Down"  { "move-viewport" (0, 1) };
}

textview
{
  -gtk-key-bindings: TextViewKeyBindings;
}

But in GTK4 I get a:

Theme parser error: style.css:90:1-13: Unknown @ rule
Theme parser error: style.css:107:3-20: No property named "-gtk-key-bindings"

What’s the way to go with this in GTK4? I found nothing on Gtk3->4 migration guide and there’s no Gtk – 3.0: Key Bindings on GTK4 docs, so I supposed this feature was removed.

Thanks in advance for any help.

Yes, the “key themes” were removed. There is no replacement, as of this moment. There’s also no plan to re-introduce a way to change existing key bindings. See this comment on GitLab: drop keyboard themes (#1669) · Issues · GNOME / gtk · GitLab

I always found this way of defining shortcuts on CSS files confusing and IMO was a good decision to not move with it to GTK4.

But…. is there not even a ugly workaround to remove these key bindings? like adding a shortcut controller with same shortcuts but doing nothing and activating another action?

GykSourceView uses keybindings that I need to use in my application and if I can’t disable them I can’t use it all, making it impossible to port my app to GTK4 :sob:

Unlike GTK3, GTK4 has a more complex input model:

You can use a custom ShortcutController for your application, and set it to capture key shortcuts; those will take the precedence over the shortcuts used by other widgets—assuming GtkSourceView does not use the capture phase itself.

1 Like

Thanks, I’ll give I try later and report the results here.

It worked since GtkSourceView use the bubble phase, thanks!!

Solution (in Crystal lang)

    sc_controller = Gtk::ShortcutController.new(propagation_phase: :capture)
    action = Gtk::CallbackAction.new(->(w  : Gtk::Widget, v : GLib::Variant?){ true })
    trigger = Gtk::ShortcutTrigger.parse_string("<Alt>Up|<Alt>Right|<Alt>Down|<Alt>Left")
    shortcut = Gtk::Shortcut.new(action: action, trigger: trigger)
    sc_controller.add_shortcut(shortcut)

    add_controller(sc_controller)
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.