如何在GTK应用程序中拦截系统按键

时间:2019-01-20 14:52:04

标签: gtk vala

我正在使用Vala GTK应用程序,该应用程序默认情况下会启动最小化,并且我想绑定特定的关键字快捷方式以将最小化窗口置于最前面。

当应用程序被聚焦时,我能够使用加速器来处理键盘事件,但是当应用程序最小化时,我无法从系统中截获任何按键。

如何使应用监听系统键盘事件,以便能够相应地检测到按键按下?

谢谢。

1 个答案:

答案 0 :(得分:2)

我查看了Ideogram的源代码,以了解其如何将Super-e注册为热键。它看起来基本上是以下内容,包括首先检查是否尚未为该应用程序注册自定义热键。

// 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);
    }
}

它使用CustomShortcutSettingsincluded in its source来处理对系统设置的读取和写入。该类源自另一个名为Clipped的应用程序。

相关问题