Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 822
sangorys Avatar asked Sep 17 '14 21:09

sangorys


People also ask

How do I find the contents of a clipboard in Python?

You can use pandas. read_clipboard() to read the clipboard contents as a DataFrame . It is very useful when used with IPython or Jupyter Notebook. to_clipboard() is also provided to copy the contents of a DataFrame to the clipboard.

Can Python access the clipboard?

In Python, you can copy text (string) to the clipboard and paste (get) text from the clipboard with pyperclip. You can also monitor the clipboard to get the text when updated. asweigart/pyperclip: Python module for cross-platform clipboard functions.


1 Answers

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()
like image 99
sangorys Avatar answered Oct 02 '22 22:10

sangorys