gpt4 book ai didi

javascript - GJS:Gtk.TextView 按键事件不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 04:51:03 25 4
gpt4 key购买 nike

我正在尝试使用 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

有人能告诉我我做错了什么吗?谢谢!

最佳答案

Gdk.Event 不包含属性 typekeyval,这就是它们 undefined 的原因。它存在的时间不长,但现在在 https://people.gnome.org/~gcampagna/docs 上提供了 GObject Introspection 绑定(bind)到 Gjs 的文档。 .

从您的打印输出中您可以看到 event 是一个 Gdk.Event 并且其文档位于 https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.Event.html。 .在那里你可以看到有函数 get_event_typeget_keyval。第一个返回 Gdk.EventType ( https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.EventType.html ),后者返回一个数组,其中第二个元素包含按下的键的数字代码。您可以将数字键与 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);

获得一些合理的输出。

关于javascript - GJS:Gtk.TextView 按键事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30655586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com