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

前端之家收集整理的这篇文章主要介绍了javascript – GJS:Gtk.TextView按键事件不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用gjs为gnome- shell创建简单的gtk应用程序.

它的窗口只包含Gtk.TextView,我想在用户输入时处理事件.

这是我的代码

  1. #!/usr/bin/gjs
  2.  
  3. var Gtk = imports.gi.Gtk;
  4.  
  5. function MainWindow () {
  6. this._init ();
  7. }
  8.  
  9. MainWindow.prototype = {
  10. _init: function () {
  11. this.window = new Gtk.Window ({
  12. title: "Just Calculator",window_position: Gtk.WindowPosition.CENTER,default_height: 400,default_width: 440,});
  13.  
  14. //this.window.show ();
  15. this.window.connect ("hide",Gtk.main_quit);
  16. this.window.connect ("delete-event",function () {
  17. Gtk.main_quit();
  18. return true;
  19. });
  20.  
  21. this.textBox = new Gtk.TextView();
  22. this.textBox.connect('key-press-event',this._keyPress);
  23.  
  24. var sw = new Gtk.ScrolledWindow ({shadow_type:Gtk.ShadowType.IN});
  25. sw.add (this.textBox);
  26. this.window.add(sw);
  27.  
  28. this.window.show_all();
  29. },_keyPress: function(textview,event) {
  30. print(event,event.type,event.keyval);
  31. textview.buffer.text = 'ok';
  32. return true;
  33. }
  34. }
  35.  
  36. Gtk.init (null,null);
  37. var window = new MainWindow ();
  38. Gtk.main ();

它通常工作,但我无法读取event.keyval:控制台输出是“未定义”:

  1. [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_开头的常量进行比较.

例如,在代码顶部添加一些导入

  1. var Gdk = imports.gi.Gdk;
  2. var Clutter = imports.gi.Clutter;

并将记录行更改为

  1. print(event,event.get_event_type() === Gdk.EventType.KEY_PRESS,event.get_keyval()[1] === Clutter.KEY_Escape);

获得一些合理的输出.

猜你在找的JavaScript相关文章