我试图获取键盘修饰符状态,将此GDK示例here移植到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()
答案 0 :(得分:3)
在OO术语中,您尝试在类Gdk.Keymap
上调用方法,而不是在对象实例this._keymap
上调用。这对我有用:
this._keymap = Gdk.Keymap.get_default ();
this._caps = this._keymap.get_modifier_state ();