Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling window focus events in a gnome shell extension

I am developing a gnome shell extension for Gnome 3.4. My extension needs to capture the window events if any editable text is focused in/out.

global.stage.connect('notify::focus-key', Lang.bind(this, this._myHandler));

did not work for me.

Here is a simple use-case: whenever user clicks on firefox search box, I want my handler to be run.

Thanks for any help,

like image 202
The_Cute_Hedgehog Avatar asked Oct 31 '25 21:10

The_Cute_Hedgehog


1 Answers

Selcuk pointed me this question, so in order to have this answered here for future search.

The library that would allow to set a global-desktop listener to focus changes is libatspi (the client-side library of GNOME accessibility framework). You could use directly C, pyatspi2 (python manual bindings) or gobject-introspection based bindings (ie, javascript). So a small javascript program that prints name:role_name of the focused object each time the focus change would be:

const Atspi = imports.gi.Atspi;

function onChanged (event) {
    log(event.source.get_name() + ',' + event.source.get_role_name());
}

Atspi.init();
let atspiListener = Atspi.EventListener.new(onChanged);
atspiListener.register("object:state-changed:focused");
Atspi.event_main();

In any case, for code examples, you could take a look to recently added focus/caret tracking feature on gnome-shell magnifier (small-size example using javascript) or Orca (GNOME screen reader, big-size example, uses pyatspi2).

libatspi reference here: libatspi Reference Manual

gnome-shell magnifier code here

like image 122
infapi00 Avatar answered Nov 04 '25 08:11

infapi00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!