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

I am working on a clipboard manager. My current problem is successfully modifying the clipboard from any application. For instance:

  • Using ctrl - c
  • Right click and copy to clipboard

The idea is that a python script runs in the background like a deamon, and catches every clipboard change

Thank you very much:)

PS: For people who know autohotkey, I'm looking for the equivalent of onClipboardChange

+6
source share
3 answers

I found a solution using GTK on the Internet. He works:)

import sys 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() 

Does anyone have a solution with QT or a more proprietary solution?

+5
source

I think you will need to poll the clipboard ... I don’t think you can listen to the event

here is an example in windows

 import win32clipboard last_data = None while True: # get clipboard data win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() if data != last_data: print "Clipboard Changed!" + data last_data = data 
0
source

Do it with C # / Mono and Gtk.

  /* put this eventhandler on your main method */ Gtk.Clipboard.Get (Gdk.Selection.Clipboard).OwnerChange += onClipboardOwnerChange; protected void onClipboardOwnerChange (object sender, EventArgs e) { Console.WriteLine ("onClipboardOwnerChange!!"); } 
0
source

Source: https://habr.com/ru/post/975454/


All Articles