brpaz
brpaz

Reputation: 3658

How to intercept system keypress in a GTK application

I am working on a Vala GTK application that starts minimized by default and I want to bind a specific keyword shortcut to bring the minimized window to the front.

I am able to handle Keyboard events using Accelerators when the app is focus ed, but I am unable to intercept any key press from the system when the app is minimized.

How can I make the app listen to system keyboard events so I can detect the key press accordingly?

Thank you.

Upvotes: 1

Views: 413

Answers (1)

ianmjones
ianmjones

Reputation: 3415

I took a look at the source for Ideogram to see how it registers Super-e as a hot-key. It looks to be basically the following, including first checking that a custom hot-key has not already been registered for the application.

// These constants are set at class level.
public const string SHORTCUT = "<Super>e";
public const string ID = "com.github.cassidyjames.ideogram";

// Set shortcut within activate method.
CustomShortcutSettings.init ();
bool has_shortcut = false;
foreach (var shortcut in CustomShortcutSettings.list_custom_shortcuts ()) {
    if (shortcut.command == ID) {
        has_shortcut = true;
            return;
    }
}
if (!has_shortcut) {
    var shortcut = CustomShortcutSettings.create_shortcut ();
    if (shortcut != null) {
        CustomShortcutSettings.edit_shortcut (shortcut, SHORTCUT);
        CustomShortcutSettings.edit_command (shortcut, ID);
    }
}

It uses a CustomShortcutSettings class included in its source to handle the reading and writing to the system settings. The class originated in another application called Clipped.

Upvotes: 2

Related Questions