Integration of Gtk and XLib using Python

So after nobody cared to reply Assign a global hotkey Im posting a new topic that should atleast get me some help.
I made a program that listens for keypress using Xlib but I can’t combine the main loop of Xlib with that of Gtk. Any help on that is welcome.
Or simply, all I want is to make a global hotkey for my Gtk app that stays as an indicator.
Please someone reply atleast this topic :slightly_smiling_face:

Here’s me replying… :slight_smile:

Seriously, though, I know nothing about integrating global hotkeys with GTK. The best I can offer is a blog post about AccelGroups. Oh, and it’s for the D language, so any answers you find will have to be ported.

Well, at least you got a reply. :slight_smile:

1 Like

:smile: Thanks, I will check it out. Sometimes I find some issues have solutions in languages which are rarely used and popular languages are just not mentioned at all :laughing: It happens…
By the way, thanks for your attention, hope someone else should have some idea.

Oops :sweat_smile: GtkD is your website, sorry if my “rarely used language” comment was offensive…

Yeah, no worries. I know D isn’t as mainstream as we affectionados would like. Still, syntax-wise it sits mid-way between C and C++, so it shouldn’t be hard to go in either direction.

1 Like

try Keybinder.

on gnome wayland try this .


Have you considered asking google?

One of my first hit was

Well again Dlang – dlang seems to be the most active language currently for GTK.


I did did a Internet search (okay I use DuckDuckGo and not Google) and I got results from StackOverflow and so on, Google uses personalized results, so if I’m right, you should have some sort of connection with dlang or something, but its not what I’m into :sweat_smile:, thanks buddy, I will check it

Oh yea but my question is for Linux

Sounds promising!
By “Keybinder” do you mean the keybinder module? If yes, I will research about it. I actually thought of solving things with just XLib and Gtk (and Python) without use of other modules, but yea, thanks a lot bro!

1 Like

Wait what??

gi.repository has a keybinder module??? Awesome! I will check it out!

1 Like

Keybinder use Gobject-Introspection .

u need first to install keybinder3 .

fedora :

sudo dnf install keybinder3

Ubuntu :

sudo apt install gir1.2-keybinder-3.0

1 Like

Yup. I actually knew Keybinder as a part of Kupfer project, but I never thought that it was the official “keybinding module” of GI.
Then why can’t we add that package and its sourcecode to official GObject-Introspection repository (i.e. the place where development of Gtk, Gdk, etc. takes place) and take care of its development there?
Okay anyway, thanks for this answer, I will “accept” your answer after I’m successfully able to achieve what I wanted.
Also others, thanks for your help and you are more than welcome if you got any other cool way to do it. :smile:

There is no such thing as an “official” module of GI. GI provides the code to load introspection data generated by GObject-based libraries, nothing more.

1 Like

Okay thanks for the correction !:pray:

Use keybinder module:
Example usage:

import gi
gi.require_versions({"Gtk": "3.0", "Gdk": "3.0", "Keybinder": "3.0"})
from gi.repository import Gtk, Keybinder

class A:
    def __init__(self): = Gtk.Window()
        self.lab = Gtk.Label(label="Hello")"destroy", Gtk.main_quit)
        # Basic setup of a window with a label

        self.count = 0

        key = Keybinder  # The module to bind keys
        key.bind("<control>m", self.say, "Hello")
        # key.bind(KEYCOMBINATION, FUNC, ARG)
        key.init()  # Call the mainloop something like Gtk.main()

    def say(self, key, msg):
        self.lab.set_label(f"Pressed {key} {self.count} times")
        self.count += 1

A()  # Call the object
Gtk.main()  # Call the main loop

This bare-bone example should help you. Thanks a lot @youssefmsourani and others of this post for their contributions :smile:


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.