【问题标题】:How to intercept system keypress in a GTK application如何在 GTK 应用程序中拦截系统按键
【发布时间】:2019-01-21 17:42:10
【问题描述】:

我正在开发一个默认情况下开始最小化的 Vala GTK 应用程序,我想绑定一个特定的关键字快捷方式以将最小化的窗口置于最前面。

当应用程序获得焦点时,我可以使用加速器处理键盘事件,但是当应用程序最小化时,我无法拦截来自系统的任何按键。

如何让应用监听系统键盘事件,以便相应地检测按键?

谢谢。

【问题讨论】:

    标签: gtk vala


    【解决方案1】:

    我查看了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 的应用程序。

    【讨论】:

    • 哦,所以它会在系统设置中自动注册一个全局快捷方式?聪明的。 ;) 当我有时间时,我会尝试这种方法,如果可行,我会提供反馈。
    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多