【发布时间】:2019-01-21 17:42:10
【问题描述】:
我正在开发一个默认情况下开始最小化的 Vala GTK 应用程序,我想绑定一个特定的关键字快捷方式以将最小化的窗口置于最前面。
当应用程序获得焦点时,我可以使用加速器处理键盘事件,但是当应用程序最小化时,我无法拦截来自系统的任何按键。
如何让应用监听系统键盘事件,以便相应地检测按键?
谢谢。
【问题讨论】:
我正在开发一个默认情况下开始最小化的 Vala GTK 应用程序,我想绑定一个特定的关键字快捷方式以将最小化的窗口置于最前面。
当应用程序获得焦点时,我可以使用加速器处理键盘事件,但是当应用程序最小化时,我无法拦截来自系统的任何按键。
如何让应用监听系统键盘事件,以便相应地检测按键?
谢谢。
【问题讨论】:
我查看了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);
}
}
它使用CustomShortcutSettings 类included in its source 来处理对系统设置的读取和写入。该类源自另一个名为 Clipped 的应用程序。
【讨论】: