GJS: Gtk.TextView key-press-event does not work
Asked Answered
S

1

10

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!

Selectee answered 4/6, 2015 at 22:56 Comment(2)
Did you manage to fix this?Lissy
try this: _keyPress: function(self, textview, event)Bevel
C
2

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.

Cheju answered 13/12, 2015 at 13:49 Comment(2)
hi, it seems dead linkRockyrococo
The new place to find GJS docs is gjs-docs.gnome.org, hopefully longer lived since it's in an official place and not an individual persons place.Cheju

© 2022 - 2024 — McMap. All rights reserved.