找到你要的答案

Q:Gnome GJS Error: Gdk.Keymap.get_modifier_state is not a function

Q:侏儒GJS错误:gdk.keymap.get_modifier_state不是一个函数

I'm trying to get the keyboard modifier state, porting this GDK example here to Gnome GJS to use it in a Gnome extension.

The code below is modified official demo from https://developer.gnome.org/gnome-devel-demos/stable/hellognome.js.html.en .

The problem is Gdk.Keymap.get_modifier_state() reported as not a function where Gdk.Keymap.get_default() runs ok.

Possibly, I'm missing something in using functions with structure parameter in JS. (I'm not familiar with JS). So What's wrong with my code?

Code:

#!/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);

Error message:

(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

However, I can see it in some docs like here: http://www.roojs.org/seed/gir-1.2-gtk-3.0/seed/Gdk.Keymap.html and in GIR mapping /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>

I tried with Python, to check if the issue is with the introspection binding. Anyway, It works well.

#!/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()

我试图让键盘修改状态,将这个GDK的例子GNOME GJS使用它在一个侏儒的延伸。

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

问题是Gdk。键映射。get_modifier_state()报道不是一个函数,GDK。你所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和吉尔映射/ usr /分享/ 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()
answer1: 回答1:

In OO terms, you are trying to call a method on the class, Gdk.Keymap, rather than on the object instance, this._keymap. This worked for me:

this._keymap = Gdk.Keymap.get_default ();
this._caps = this._keymap.get_modifier_state ();

在面向对象的术语,你试图调用类方法,Gdk . Keymap,而不是对象的实例,这_keymap。这对我有用:

this._keymap = Gdk.Keymap.get_default ();
this._caps = this._keymap.get_modifier_state ();
javascript  gnome  gdk  gobject-introspection  gjs