我正在尝试使用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
有人能告诉我我做错了什么吗?
谢谢!
解决方法
@H_301_18@ Gdk.Event不包含属性类型或keyval,这就是它们未定义的原因.它已经存在很长时间了,但现在有文件可用于GObject Introspection绑定到 https://people.gnome.org/~gcampagna/docs的Gjs.从你的打印出来,你看到该事件是一个Gdk.Event,其文档是在https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.Event.html.在那里你可以看到有函数get_event_type和get_keyval.第一个返回Gdk.EventType(@L_404_3@),后者返回第二个元素包含按下的键的数字代码的数组.您可以将数字键与Clutter中以KEY_开头的常量进行比较.
- var Gdk = imports.gi.Gdk;
- var Clutter = imports.gi.Clutter;
并将记录行更改为
- print(event,event.get_event_type() === Gdk.EventType.KEY_PRESS,event.get_keyval()[1] === Clutter.KEY_Escape);
获得一些合理的输出.