Skip to content

Instantly share code, notes, and snippets.

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 yucefsourani/5fe7c53a5000bb3c883cfd2bd9cc436d to your computer and use it in GitHub Desktop.
Save yucefsourani/5fe7c53a5000bb3c883cfd2bd9cc436d to your computer and use it in GitHub Desktop.
Global Shortcut On Gnome 3.32 Wayland (Fedora 30)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#
# Copyright 2019 youcef sourani <youssef.m.sourani@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
from time import sleep
DBusGMainLoop(set_as_default=True)
loop = GLib.MainLoop()
bus = dbus.SessionBus()
gnome_obj = bus.get_object("org.gnome.Shell","/org/gnome/Shell")
ids_functions_map = dict()
def on_alt_i_clicked():
print("Alt I Clicked")
def on_alt_u_clicked():
print("Alt U Clicked")
keyboard_shortcuts_and_functions = {
"<Alt>I" : on_alt_i_clicked,
"<Alt>U" : on_alt_u_clicked
}
def on_accelerator_activated(actionid,argv):
print(argv)
ids_functions_map[actionid]()
def wayland_unbind_key_to_function():
bus.remove_signal_receiver( on_accelerator_activated, signal_name="AcceleratorActivated", dbus_interface="org.gnome.Shell", bus_name="org.gnome.Shell")
for k in ids_functions_map.keys():
print(k)
gnome_obj.UngrabAccelerator(k,dbus_interface="org.gnome.Shell")
sleep(0.5)
def wayland_bind_key_to_function():
global ids_functions_map
ids_functions_map.clear()
for shortcut,func in keyboard_shortcuts_and_functions.items():
id__ = gnome_obj.GrabAccelerator(shortcut,1,1,dbus_interface="org.gnome.Shell")
print(id__)
ids_functions_map.setdefault(id__,func)
sleep(0.5)
bus.add_signal_receiver( on_accelerator_activated, signal_name="AcceleratorActivated", dbus_interface="org.gnome.Shell", bus_name="org.gnome.Shell")
if __name__ == "__main__":
try:
if not loop.is_running():
wayland_bind_key_to_function()
loop.run()
except KeyboardInterrupt:
wayland_unbind_key_to_function()
loop.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment