[Python] Super+V works only for the second time in Keybinder library python linux mint 22.1...

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Linux mint 22.1 cinnamon edition and I'm trying to bind Super+V key combination in a python code using the Keybinder library form the gi.repository, and when I press Super+V for the first time, It just prints a lowercase letter "v" in the selected text area, and if I then press "v" again it prints "Key was pressed!", as expected. Here is my code:

    import gi
    gi.require_version('Gtk', '3.0')
    gi.require_version('Keybinder', '3.0')
    from gi.repository import Keybinder, Gtk

    class Application(Gtk.Application):
    def __init__(self):
    super().__init__(application_id="org.example.clipboardhistory", flags=0)
    self.connect("activate", self.on_activate)
    self.connect("startup", self.on_startup)

    def on_startup(self, app):
    print("Application started.")
    self.hold()

    def on_activate(self, app):
    pass

    def key_press(self, user_data=None):
    print("Key was pressed!")

    if __name__ == "__main__":
    app = Application()
    Keybinder.init()
    # Keybinder.bind("<Alt>F3", app.show_clipboard_window_action)
    Keybinder.bind("<Super>V", app.key_press)
    app.run(None)



    I asked ChatGPT about it, and it said that the code below would work fine, but it works the same way as the first one. Here is the code that ChatGPT gave me:

    from Xlib import X, XK, display
    from Xlib.ext import record
    from Xlib.protocol import rq

    # Connect to the X server
    d = display.Display()
    root = d.screen().root

    # Get keycode for 'v' key
    v_keysym = XK.string_to_keysym('v')
    v_keycode = d.keysym_to_keycode(v_keysym)

    # Get keycode for Super (usually Mod4)
    mod_mask = X.Mod4Mask

    # Grab the key combination on the root window
    root.grab_key(v_keycode, mod_mask, True,
    X.GrabModeAsync, X.GrabModeAsync)

    print("Listening for Super+V...")

    while True:
    event = d.next_event()
    if event.type == X.KeyPress:
    if event.detail == v_keycode and (event.state & mod_mask):
    print("Super+V pressed!")



    With Alt+F3, or other key combination it works fine, but Is there a way to make it work with Super key? Maybe there is another way to do this? I'm not using Gtk keypress event because I want to catch the key combination without an active window. I would appreciate any answers/comments.

    Edit: if in the menu@cinnamon.org applet I don't have Super_L assigned to the "show menu" action, the keypress works for the first time. And if I assign a command to Super+V in the Keyboard > Shortcuts menu, it'd also work for the first keypress.

    Continue reading...

Compartilhe esta Página