Search code examples
pythonclipboard

How to catch the clipboard event (onChangeClipboard equivalent) from any application in Python


I am working on a clipboard manager. My current issue is to succeed in catching modification of the clipboard from any applications. For instance :

  • From a ctrl - c
  • From a right click and copy to clipboard

The idea is that a python script is running in background, like a deamon and catch every change of the clipboard

Thank you a lot :)

PS: For people who know autohotkey, I am looking for onClipboardChange equivalent


Solution

  • I found in the web a solution using GTK:

    from gi.repository import Gtk, Gdk
    
    def callBack(*args):
        print("Clipboard changed. New value = " + clip.wait_for_text())
    
    clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
    clip.connect('owner-change', callBack)
    Gtk.main()