A Python 3 and Gtk 3.22.5 application responds to a global key binding, thanks to the Keybinder library. Currently it invokes a simple callback:
def keybinder_callback(self, keystr, user_data):
print("Handling", keystr, user_data)
Elsewhere in the application is a Gtk.Window
that contains a Gtk.MenuBar
. The keybinder_callback
needs to activate the menu bar as if the user had clicked the mouse on it.
(it's a very basic dock-type application rather than one with a typical application window)
I have tried sending a signal to a menu item:
self.menubar.get_children()[0].emit("activate-item")
without any joy. I also tried faking a button press
from Xlib.ext.xtest import fake_input
fake_input(display,X.ButtonPress,1)
which also had no effect, but feels like the wrong way to do it anyway. I thought sending a signal would be more appropriate.
Can a widget be activated programmatically as if a user mouse-clicked on it?
(it doesn't have to be a simulated mouse-click - it just needs to activate and focus the widget in the same way that a mouse-click would)
I have written an example that has a keybinder_callback
like this:
def keybinder_callback(self, keystr, user_data):
print("Handling", keystr, user_data)
print("Event time:", Keybinder.get_current_event_time())
activate_the_menu()
I need to add some command to that function that will activate_the_menu
.
I have tried many things, including capturing real events (by monitoring with xev
) and simulating them ( ENTER_NOTIFY
and FOCUS_CHANGE
), by injecting them into Gdk.main_do_event
.
I've tried calling menu.popup
, menu.popup_at_widget
, menubar.select_item
and other numerous things. All to no avail.
Running out of ideas, I've even dusted off my old Xlib book...
Incidentally, whilst not a proper solution, this works from a shell:
$ xdotool mousemove 1605 10 click 1 mousemove restore
but not reliably from the keybinder_callback
:
run("xdotool mousemove %d 10 click 1 mousemove restore" %
(self.get_position().root_x+5) , shell=True)
GtkMenu.popup_at_widget
, but be careful, I almost froze my PC while playing around with it. Not really sure what happened, but after the menu popped up I couldn't close the program anymore. – Aran-Feyself.menu.popup_at_widget(self,Gdk.Gravity.STATIC,Gdk.Gravity.STATIC,None)
but gotWarning: g_object_set_data: assertion 'G_IS_OBJECT (object)' failed
– starfry