我正在尝试使用 gjs 为 gnome-shell 创建简单的 gtk 应用程序。
它的窗口仅包含 Gtk.TextView ,我想在用户键入时处理事件。
这是我的代码:
#!/usr/bin/gjs
var Gtk = imports.gi.Gtk;
function MainWindow () {
this._init ();
}
MainWindow.prototype = {
_init: function () {
this.window = new Gtk.Window ({
title: "Just Calculator",
window_position: Gtk.WindowPosition.CENTER,
default_height: 400,
default_width: 440,
});
//this.window.show ();
this.window.connect ("hide", Gtk.main_quit);
this.window.connect ("delete-event", function () {
Gtk.main_quit();
return true;
});
this.textbox = new Gtk.TextView();
this.textbox.connect('key-press-event', this._keyPress);
var sw = new Gtk.ScrolledWindow ({shadow_type:Gtk.ShadowType.IN});
sw.add (this.textbox);
this.window.add(sw);
this.window.show_all();
},
_keyPress: function(textview, event) {
print(event, event.type, event.keyval);
textview.buffer.text = 'ok';
return true;
}
}
Gtk.init (null, null);
var window = new MainWindow ();
Gtk.main ();
它通常可以工作,但我无法读取 event.keyval:控制台输出为“未定义”:
[union instance proxy GIName:Gdk.Event jsobj@0x7f99b1027040 native@0x1dfeab0] undefined undefined
有人可以告诉我我做错了什么吗?谢谢!