1

我正在尝试获取键盘修饰符状态,将此处的 GDK 示例移植到 Gnome GJS 以在 Gnome 扩展中使用它。

下面的代码是从https://developer.gnome.org/gnome-devel-demos/stable/hellognome.js.html.en修改的官方演示。

该问题被Gdk.Keymap.get_modifier_state()报告为不是Gdk.Keymap.get_default()运行正常的函数。

可能,我在 JS 中使用带有结构参数的函数时遗漏了一些东西。(我不熟悉JS)。那么我的代码有什么问题?

代码:

#!/usr/bin/gjs

//https://developer.gnome.org/gnome-devel-demos/stable/hellognome.js.html.en

const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const Lang = imports.lang;
const Webkit = imports.gi.WebKit;

const HelloGNOME = new Lang.Class ({
    Name: 'Hello GNOME',
    _init: function () {
        this.application = new Gtk.Application ();
        this.application.connect('activate', Lang.bind(this, this._onActivate));
        this.application.connect('startup', Lang.bind(this, this._onStartup));
        },

        _onActivate: function () {
                this._window.present ();
                this._keymap = Gdk.Keymap.get_default ();
                //this._state = Gdk.Keymap.get_modifier_state (this._keymap);
                this._caps = Gdk.Keymap.get_modifier_state ();
        },

        _onStartup: function () {
                this._buildUI ();
        },

        // Build the application's UI
        _buildUI: function () {

                // Create the application window
                this._window = new Gtk.ApplicationWindow  ({
                        application: this.application,
                        title: "Welcome to GNOME",
                        default_height: 200,
                        default_width: 400,
                        window_position: Gtk.WindowPosition.CENTER });
                // Create a webview to show the web app
                this._webView = new Webkit.WebView ();

                // Put the web app into the webview
                this._webView.load_uri (GLib.filename_to_uri (GLib.get_current_dir() +
                "/hellognome.html", null));

                // Put the webview into the window
                this._window.add (this._webView);

                // Show the window and all child widgets
                this._window.show_all();
        },

});

// Run the application
let app = new HelloGNOME ();
app.application.run (ARGV);

错误信息:

(gjs:2483): Gjs-WARNING **: JS ERROR: TypeError: Gdk.Keymap.get_modifier_state is not a function
HelloGNOME<._onActivate@./gdk_mod.js:23
wrapper@resource:///org/gnome/gjs/modules/lang.js:169
@./gdk_mod.js:58

但是,我可以在一些文档中看到它,例如:http : //www.roojs.org/seed/gir-1.2-gtk-3.0/seed/Gdk.Keymap.html 和 GIR 映射/usr/share/gir-1.0/Gdk-3.0.gir

<method name="get_modifier_state"
              c:identifier="gdk_keymap_get_modifier_state"
              version="3.4">
        <doc xml:space="preserve">Returns the current modifier state.</doc>
        <return-value transfer-ownership="none">
          <doc xml:space="preserve">the current modifier state.</doc>
          <type name="guint" c:type="guint"/>
        </return-value>
        <parameters>
          <instance-parameter name="keymap" transfer-ownership="none">
            <doc xml:space="preserve">a #GdkKeymap</doc>
            <type name="Keymap" c:type="GdkKeymap*"/>
          </instance-parameter>
        </parameters>
</method>

我尝试使用 Python 来检查问题是否与自省绑定有关。无论如何,它运作良好。

#!/usr/bin/python

from gi.repository import Gdk, GLib

def update(keymap):
        print Gdk.Keymap.get_modifier_state(keymap)
        return True

if __name__=="__main__":

        #Gdk.init()

        loop = GLib.MainLoop()

        keymap = Gdk.Keymap.get_default()
        GLib.timeout_add_seconds(1, update, keymap)

        loop.run()
4

1 回答 1

3

在 OO 术语中,您试图在类上调用方法Gdk.Keymap,而不是在对象实例上调用this._keymap。这对我有用:

this._keymap = Gdk.Keymap.get_default ();
this._caps = this._keymap.get_modifier_state ();
于 2015-08-15T14:39:07.003 回答