Skip to content

Instantly share code, notes, and snippets.

@m13253
Created November 11, 2019 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m13253/93de835eb422766e3f11dbdfefbb1274 to your computer and use it in GitHub Desktop.
Save m13253/93de835eb422766e3f11dbdfefbb1274 to your computer and use it in GitHub Desktop.
A mouse test program written in gjs (Gtk+ JavaScript)
#!/usr/bin/gjs
const GObject = imports.gi.GObject;
const Gdk = imports.gi.Gdk;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const MouseTest = new Lang.Class({
Name: "Mouse Test",
_init : function() {
this.application = new Gtk.Application({
application_id: "org.example.mousetest"
});
this.application.connect("activate", Lang.bind(this, this._onActivate));
this.application.connect("startup", Lang.bind(this, this._onStartup));
},
_onActivate: function() {
this.window.present();
},
_onStartup: function() {
this._buildUI();
},
_buildUI: function () {
this.window = new Gtk.ApplicationWindow({
application: this.application,
window_position: Gtk.WindowPosition.CENTER,
title: "Mouse Test",
default_width: 640,
default_height: 480
});
this.window.set_events(
Gdk.EventMask.POINTER_MOTION_MASK |
Gdk.EventMask.BUTTON_MOTION_MASK
);
this.window.connect("destroy", function() {
Gtk.main_quit();
});
this.window.connect("motion_notify_event", this._onMouseEvent);
this.window.connect("button_press_event", this._onMouseEvent);
this.window.show();
},
_onMouseEvent: function(stage, event) {
const coords = event.get_coords();
let res = "";
if(coords[0])
res += "(" + coords[1] + ", " + coords[2] + ")";
const buttons = event.get_button();
if(buttons[0])
res += " Button: " + buttons[1];
if(res)
print(res);
}
});
let app = new MouseTest();
app.application.run(ARGV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment