Keybindings for individual letter keys (not modifier-combinations) on a GtkTextView widget (Gtk3 and PyGI)

Posted by monotasker on Ask Ubuntu See other posts from Ask Ubuntu or by monotasker
Published on 2012-09-26T21:43:22Z Indexed on 2012/09/26 21:49 UTC
Read the original article Hit count: 348

I've been able to set several keybord shortcuts for a GtkTextView and a GtkTextEntry using the new css provider system. I'm finding, though, that I only seem to be able to establish keybindings for combinations including a modifier key. The widget doesn't respond to any bindings I set up that use:

  • the delete key
  • the escape key
  • individual letter or punctuation keys alone

Here's the code where I set up the css provider for the keybindings:

   #set up style context
    keys = Gtk.CssProvider()
    keys.load_from_path(os.path.join(data_path, 'keybindings.css'))

    #set up style contexts and css providers
    widgets = {'window': self.window, 'vbox': self.vbox,
                'toolbar': self.toolbar, 'search_entry': self.search_entry,
                'paned': self.paned,
                'notelist_treeview': self.notelist_treeview,
                'notelist_window': self.notelist_window,
                'notetext_window': self.notetext_window,
                'editor': self.editor,
                'statusbar': self.statusbar
                }
    for l, w in widgets.iteritems():
        w.get_style_context().add_provider(keys,
                                        Gtk.STYLE_PROVIDER_PRIORITY_USER)

Then in keybindings.css this is an example of what works:

@binding-set gtk-vi-text-view
{
    bind "<ctrl>b" { "move-cursor" (display-lines, -5, 0) };     /* 5 lines up */
    bind "<ctrl>k" { "move-cursor" (display-lines, -1, 0) };     /* down */
    bind "<ctrl>j" { "move-cursor" (display-lines, 1, 0) };      /* up */
}

Part of what I'm trying to do is just add proper delete-key function to the text widgets (right now the delete key does nothing at all). So if I add a binding like one of these, nothing happens:

bind "Delete" { "delete-selection" () };
bind "Delete" { "delete-from-cursor" (chars, 1) };

The other part of what I want to do is more elaborate. I want to set up something like Vim's command and visual modes. So at the moment I'm just playing around with (a) setting the widget to editable=false by hitting the esc key; and (b) using homerow letters to move the cursor (as a proof-of-concept exercise). So far there's no response from the escape key or from the letter keys, even though the bindings work when I apply them to modifier-key combinations.

For example, I do this in the css for the text-widget:

bind "j" { "move-cursor" (display-lines, 1, 0) };            /* down */
bind "k" { "move-cursor" (display-lines, -1, 0) };           /* up */
bind "l" { "move-cursor" (logical-positions, 1, 0) };        /* right */
bind "h" { "move-cursor" (logical-positions, -1, 0) };       /* left */

but none of these bindings does anything, even if other bindings in the same set are respected.

What's especially odd is that the vim-like movement bindings above are respected when I attach them to a GtkTreeView widget for navigating the tree-view options:

@binding-set gtk-vi-tree-view {
    bind "j" { "move-cursor" (display-lines, 1) };               /* selection down */
    bind "k" { "move-cursor" (display-lines, -1) };              /* selection up */ }

So it seems like there are limitations or overrides of some kind on keybindings for the TextView widget (and for the del key?), but I can't find documentation of anything like that. Are these just things that can't be done with the css providers? If so, what are my alternatives for non-modified keybindings?

Thanks.

© Ask Ubuntu or respective owner

Related posts about shortcut-keys

Related posts about application-development