代码之家  ›  专栏  ›  技术社区  ›  gcb

如何在gnomeshell扩展中处理键盘事件?

  •  26
  • gcb  · 技术社区  · 11 年前

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

    我找不到任何例子。这个 documentation 提到了 keyboard 模块,但使用通用名称搜索很困难。

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

    (作为上面链接的文档中的引用阅读。它被设置为代码样式,因为出于某种原因,引用样式在本网站中不保留换行符)

    我发现一些扩展使用下面的代码来获得与我所问的结果类似的结果,但我再次未能找到特定类和方法的文档:

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

    此外,没有名为的类 键盘 任何地方。。。

    --

    edit1:更多搜索。。。我想我可能不得不使用 Clutter 应用程序编程接口。但同样,没有太多的例子或文档。我走得最远的是这个

    edit2:更多搜索。在主ui树上查看gnomeshell源代码,我认为答案是使用前面提到的 global 对象,该对象可用于扩展代码。例如

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

    2 回复  |  直到 9 年前
        1
  •  5
  •   andy.holmes    8 年前

    我在gcampax的 gtk-js-app template 前段时间,这可能与你正在做的事情有关:

    // 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,但同样的限制可能会影响gnomeshell扩展。

    更新

    我最近一直在做一些键绑定的事情,如果将信号处理程序附加到全局对象是有效的,那么可以这样做:

    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 keybinding code here 一些 shell-global documentation here 这可能会给你更多的线索。希望我能帮更多的忙,但我正在为自己的GJS atm机奋斗;)

    附录

    有一个 good answer here 带有带有信息日志记录的示例类以及推测性解释。我还发现该功能通过DBus公开,在某些情况下可能更方便:

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

    相关方法:

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

    信号:

    • AcceleratorActivate(UInt32, Dict of {String, Variant})
        2
  •  0
  •   Dharman vijay    6 年前

    为了我 global.stage.connect("key-press-event", _handleKeyPress) 成功了