In previous posts, ["Define UI comprising Dropdown in Blueprint for modern GTK apps"][dropdown-post] and ["Define UI comprising ListView in Blueprint for modern GTK apps"][listview-post], I presented how to use list widgets in [Blueprint][blueprint]. Now we look at a different, but more compact topic, the keyboard shortcut.
In a desktop app, the user is expected to do a lot of work with the keyboard alone, not always reaching for the mouse. So a good app should have many keyboard shortcuts. For example, a Ctrl+V to paste, a Ctrl+S to save, an F1 to show help, etc. Most GUI toolkits have built-in support for this. GTK is no exception. There are two ways to assign a keyboard shortcut to an action in GTK 4:
- The simplest one is the application's accelerator, set via [
gtk_application_set_accels_for_action()][set-accels]. You define the action on the application (with theapp.prefix), then call that function with a list of accelerator strings. But this is not declarative, you have to write code. - A more flexible way is to use a [
Gtk.ShortcutController][shortcut-controller], to which you add one or more [Gtk.Shortcut][shortcut] objects. EachShortcutbinds a trigger (a key combination) to an action. This is what we will learn in this post, because it is declarative and we can describe the shortcut in the Blueprint file alongside the rest of the UI.
