use*_*742 6 linux hotkeys key-bindings gnome-shell gnome-shell-extensions
(全局)将键组合(例如<Super>+A)绑定到gnome shell扩展中的函数的最简单方法是什么?
检查了几个扩展,我遇到了以下代码:
global.display.add_keybinding('random-name',
new Gio.Settings({schema: 'org.gnome.shell.keybindings'}),
Meta.KeyBindingFlags.NONE,
function() { /* ... some code */ });
Run Code Online (Sandbox Code Playgroud)
我知道组合键是由schema参数指定的,并且可以创建描述组合的XML文件.有更简单的方法吗?
这个问题很老了,但我刚刚为 Gnome Shell 40 实现了这个问题。所以这就是我是如何做到的。
该密钥在您用于扩展设置的普通模式文件中定义。所以它看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema id="org.gnome.shell.extensions.mycoolstuff" path="/org/gnome/shell/extensions/mycoolstuff/">
<key name="cool-hotkey" type="as">
<default><![CDATA[['<Ctrl><Super>T']]]></default>
<summary>Hotkey to open the cool stuff.</summary>
</key>
... other config options
</schema>
</schemalist>
Run Code Online (Sandbox Code Playgroud)
键类型是“字符串数组”,因此您可以为操作配置多个组合键。
在您的代码中,您可以像这样使用它:
const Main = imports.ui.main;
const Meta = imports.gi.Meta
const Shell = imports.gi.Shell
const ExtensionUtils = imports.misc.extensionUtils;
...
let my_settings = ExtensionUtils.getSettings("org.gnome.shell.extensions.mycoolstuff");
Main.wm.addKeybinding("cool-hotkey", my_settings,
Meta.KeyBindingFlags.IGNORE_AUTOREPEAT,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW
this._hotkeyActionMethod.bind(this));
Run Code Online (Sandbox Code Playgroud)
我建议在扩展被禁用时删除按键绑定。不知道如果你不这样做会发生什么。
Main.wm.removeKeybinding("cool-hotkey");
Run Code Online (Sandbox Code Playgroud)
顺便说一句:对设置的更改(通过 dconf 编辑器、gsettings 或您的扩展首选项)会立即生效。
小智 3
以下是我的答案的副本, 我只在 Gnome 3.22 中测试过这个
这是一个类:
KeyManager: new Lang.Class({
Name: 'MyKeyManager',
_init: function() {
this.grabbers = new Map()
global.display.connect(
'accelerator-activated',
Lang.bind(this, function(display, action, deviceId, timestamp){
log('Accelerator Activated: [display={}, action={}, deviceId={}, timestamp={}]',
display, action, deviceId, timestamp)
this._onAccelerator(action)
}))
},
listenFor: function(accelerator, callback){
log('Trying to listen for hot key [accelerator={}]', accelerator)
let action = global.display.grab_accelerator(accelerator)
if(action == Meta.KeyBindingAction.NONE) {
log('Unable to grab accelerator [binding={}]', accelerator)
} else {
log('Grabbed accelerator [action={}]', action)
let name = Meta.external_binding_name_for_action(action)
log('Received binding name for action [name={}, action={}]',
name, action)
log('Requesting WM to allow binding [name={}]', name)
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL)
this.grabbers.set(action, {
name: name,
accelerator: accelerator,
callback: callback
})
}
},
_onAccelerator: function(action) {
let grabber = this.grabbers.get(action)
if(grabber) {
this.grabbers.get(action).callback()
} else {
log('No listeners [action={}]', action)
}
}
})
Run Code Online (Sandbox Code Playgroud)
这就是你使用它的方式:
let keyManager = new KeyManager()
keyManager.listenFor("<ctrl><shift>a", function(){
log("Hot keys are working!!!")
})
Run Code Online (Sandbox Code Playgroud)
你将需要进口:
const Lang = imports.lang
const Meta = imports.gi.Meta
const Shell = imports.gi.Shell
const Main = imports.ui.main
Run Code Online (Sandbox Code Playgroud)
I might be terribly wrong, but that what I've figured out in last couple days.
First of all it is Mutter who is responsible for listening for hotkeys. Mutter is a framework for creating Window Managers, it is not an window manager itself. Gnome Shell has a class written in JS and called "Window Manager" - this is the real Window Manager which uses Mutter internally to do all low-level stuff. Mutter has an object MetaDisplay. This is object you use to request listening for a hotkey. But! But Mutter will require Window Manager to approve usage of this hotkey. So what happens when hotkey is pressed? - MetaDisplay generates event 'filter-keybinding'. - Window Manager in Gnome Shell checks if this hotkey allowed to be processed. - Window Manager returns appropriate value to MetaDisplay - If it is allowed to process this hotkey, MetaDisplay generates event 'accelerator-actived' - Your extension must listen for that event and figure out by action id which hotkey is activated.
| 归档时间: |
|
| 查看次数: |
1272 次 |
| 最近记录: |