Skip to content

Instantly share code, notes, and snippets.

@SkyzohKey
Last active June 14, 2016 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SkyzohKey/6726f2414415e3078c28ee4724182081 to your computer and use it in GitHub Desktop.
Save SkyzohKey/6726f2414415e3078c28ee4724182081 to your computer and use it in GitHub Desktop.
This sample code permits to make application-wide keyboard shortcuts using AccelGroup and Action Signals in Vala.
[Signal (action = true)]
private signal void change_chat_up ();
[Signal (action = true)]
private signal void change_chat_down ();
public MainWindow () {
this.init_keyboard_shortcuts ();
}
private void init_keyboard_shortcuts () {
Gtk.AccelGroup accel_group = new Gtk.AccelGroup ();
this.add_accel_group (accel_group);
/**
* Keyboard shortcut for switching to previous/next contact's chatview.
* FIXME: Ctrl+Up | Ctrl+Down doesn't call these signals.
**/
this.change_chat_up.connect (() => {
var index = this.selected_row.get_index ();
if (index == 0) { return; }
var prev_row = this.friendlist.get_row_at_index (index - 1);
this.friendlist.select_row (prev_row);
});
this.change_chat_down.connect (() => {
var index = this.selected_row.get_index ();
if (index == 0) { return; }
var next_row = this.friendlist.get_row_at_index (index + 1);
this.friendlist.select_row (next_row);
});
/**
* Shortcut for Ctrl+Up: Change the chat view to the previous one.
**/
this.add_accelerator ("change-chat-up", accel_group, Gdk.keyval_from_name("Up"),
Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE);
/**
* Shortcut for Ctrl+Down: Change the chat view to the next one.
**/
this.add_accelerator ("change-chat-down", accel_group, Gdk.keyval_from_name("Down"),
Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment