What I want to do and why
I want my window to unfocus, so the previous focused window is selected.
Why? I want to interact with the previously selected window (from other programs). My current plan is: unfocus my window, use libxdo
to simulate keystrokes, then focus my window again.
My window can be set on top to help avoid flicking. Should be good enough. Looks simple to me. But I can't get it to work.
What I've tried so far
Hiding the window with Gtk.Widget.hide()
and then showing it again: The window flickers too much and it's slightly moved some pixels to the top (because of window manager stubbornness, I suppose).
Sample test code
Current code calls Gtk.Window.set_focus(None)
that does not work. I need to replace that line with something else that makes what I want it to do.
losefocus.py
:
import signal
from gi import require_version
require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk, GObjectclass LoseFocusHandler:def onClick(self, window):print "Losing focus yet?"window1 = builder.get_object("window1")window1.set_focus(None)if __name__ == "__main__":GObject.threads_init()builder = Gtk.Builder()builder.add_from_file("losefocus.glade")builder.connect_signals(LoseFocusHandler())window1 = builder.get_object("window1")window1.show_all()signal.signal(signal.SIGINT, signal.SIG_DFL)Gtk.main()
losefocus.glade
:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface><requires lib="gtk+" version="3.10"/><object class="GtkWindow" id="window1"><property name="can_focus">False</property><property name="window_position">center-always</property><property name="gravity">center</property><child><object class="GtkButton" id="button1"><property name="label" translatable="yes">Lose Focus!</property><property name="visible">True</property><property name="can_focus">True</property><property name="receives_default">True</property><property name="relief">half</property><signal name="clicked" handler="onClick" swapped="no"/></object></child></object>
</interface>