如何在gnome Shell扩展中处理键盘事件? [英] How to handle keyboard events in gnome shell extensions?

查看:143
本文介绍了如何在gnome Shell扩展中处理键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加事件或其他方法来监听gnome shell扩展上的按键?例如显示一个对话框,其中每个按键都显示按下的键?

How can I add an event or other method to listen to keypresses on a gnome shell extension? e.g. show a dialog with each key press showing the pressed key?

我找不到任何示例. 文档提到了keyboard模块,但是使用通用名称搜索很难

I can not find any example. The documentation mentions a keyboard module, but with that common name searching is hard.

Class explanation
...
- General utils
   - Keyboard: Manage and define the keyboard events, etc. for gnome shell. 

(以上是从上面链接的文档中引用的引文.它被设置为代码样式,因为由于某些原因,引号样式不会保留该站点中的换行符)

(read above as a quote from the docs linked above. it is styled as code because the quote styling for some reason do not preserve line breaks in this site)

我发现了一些使用波纹管代码的扩展,其结果与我要求的类似,但我再次未能找到特定类和方法的文档:

I found some extensions using the bellow code for results similar to what i'm asking, but i, again, failed to find docs for the specific classes and methods:

workViewInjections['_init'] = injectToFunction(WorkspacesView.WorkspacesView.prototype, '_init', function(width, height, x, y, workspaces) {
        this._pickWorkspace = false;
        this._pickWindow = false;
        this._keyPressEventId = global.stage.connect('key-press-event', Lang.bind(this, this._onKeyPress));                                                                                
        this._keyReleaseEventId = global.stage.connect('key-release-event', Lang.bind(this, this._onKeyRelease));
        connectedSignals.push({ obj: global.stage, id: this._keyPressEventId });
        connectedSignals.push({ obj: global.stage, id: this._keyReleaseEventId });
        });

此外,那里没有任何名为keyboard的类...

Also, no class named keyboard anywhere there...

-

edit1:更多搜索...我认为我可能不得不使用Clutter api.但同样,没有太多的例子或文档.我走得最远的就是这个

edit1: more searching... i think i may have to use the Clutter api. but again, not much examples or documentation for that. farthest i went was this

edit2:更多搜索.在gnome外壳程序源代码上,在主ui树上,我认为答案是使用扩展代码可用的几乎没有提到的global对象.例如

edit2: more searching. looking on the gnome shell source code, on the main ui tree, i think the answer is to use the barelly mentioned global object that is available to the extension code. e.g.

global.connect('key-press-event', function(if, i, know, the, signature){} );

推荐答案

我在gcampax的

I came across this snippet in gcampax's gtk-js-app template some time ago, which may be related to what you're doing:

// Due to limitations of gobject-introspection wrt GdkEvent and GdkEventKey,
// this needs to be a signal handler
this.connect('key-press-event', Lang.bind(this, this._handleKeyPress));

_handleKeyPress: function(self, event) {
    return this.main_search_bar.handle_event(event);
},

我还不需要使用键盘事件,这是GJS中的Gtk,但是相同的限制可能会影响gnome-shell扩展.

I haven't had a need to use keyboard events yet, and this is Gtk in GJS, but the same limitation may be affecting gnome-shell extensions.

更新

最近我一直在做一些键绑定工作,如果将信号处理程序附加到全局对象上工作正常,则可以执行以下操作:

I've been doing some keybinding stuff lately, and if attaching a signal handler to the global object is working, you can do something like this:

global.display.connect("key-press-event", (widget, event, user_data) => {
    let [success, keyval] = event.get_keyval(); // integer
    let keyname = Gdk.keyval_name(keyval); // string keyname

    if (keyname === "Control_L") {
        // Dialog code or eg. this.keys_array.push("<Ctrl>");
    }
});

还有一些 Shell键绑定此处的代码 some shell -此处的全球文档可能会为您提供更多线索.希望我能提供更多帮助,但我正在努力争取自己的GJS atm;)

There's also some Shell keybinding code here and some shell-global documentation here that might give you more clues. Wish I could help more but I'm wrestling my own GJS atm ;)

添加

这里有一个很好的答案,其中包含一个示例类信息丰富的日志记录,以及推测性的解释.我还发现此功能是通过DBus公开的,在某些情况下可能更方便:

There is a good answer here with an example class with informative logging, as well as a speculative explanation. I've also found this functionality is exposed over DBus which might be more convenient in some cases:

总线名称:org.gnome.Shell->路径:/org/gnome/Shell->接口:org.gnome.Shell

Bus Name: org.gnome.Shell -> Path: /org/gnome/Shell -> Interface: org.gnome.Shell

相关方法:

  • GrabAccelerator(String accelerator, UInt32 flags)-> (UInt32 action)
  • UngrabAccelerator(UInt32 action)-> (Boolean success)
  • GrabAccelerator(String accelerator, UInt32 flags) -> (UInt32 action)
  • UngrabAccelerator(UInt32 action) -> (Boolean success)

信号:

  • AcceleratorActivate(UInt32, Dict of {String, Variant})

这篇关于如何在gnome Shell扩展中处理键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆