I am trying to create simple gtk application for gnome-shell using gjs.
Its window contains Gtk.TextView only and I want to process events when user is typing.
Here is my code:
#!/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 ();
It works generally but I can not read event.keyval: console output is "undefined":
[union instance proxy GIName:Gdk.Event jsobj@0x7f99b1027040 native@0x1dfeab0] undefined undefined
Could someone tell me what I am doing wrong? Thanks!
Gdk.Event
does not contain properties type
or keyval
and that's why they are undefined
. It hasn't been around for that long, but now there is documentation available for GObject Introspection bindings to Gjs at https://people.gnome.org/~gcampagna/docs.
From your print out you see that event
is a Gdk.Event
and the documentation for that is at https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.Event.html. There you can see that there are function get_event_type
and get_keyval
. The first returns a Gdk.EventType
(https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.EventType.html) and the latter an array where the second element contains a numeric code for the pressed key. You can compare the numeric keys to constants in Clutter that begin with KEY_
.
For example add some imports to the top of your code
var Gdk = imports.gi.Gdk;
var Clutter = imports.gi.Clutter;
and change the logging line to
print(event,
event.get_event_type() === Gdk.EventType.KEY_PRESS,
event.get_keyval()[1] === Clutter.KEY_Escape);
to get some sensible output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With