I’ve got two code samples. The first is to get current clipboard content and print it, the second is using python-keybinder to do some action on a hotkey press. I’m stuck with combining those two together. I want my clipboard content to be printed on a hotkey press (i.e. I do a text selection, press a hotkey and this selection is printed). Here’s my code:
To get selection:
import gtk
def _clipboard_changed(clipboard, event):
text = clipboard.wait_for_text()
print text
gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY).connect("owner-change", _clipboard_changed)
gtk.main()
To bind a hotkey:
import gtk
import keybinder
def callback():
print "pressed"
gtk.main_quit()
if __name__ == '__main__':
keystr = "<Ctrl>A"
keybinder.bind(keystr, callback)
gtk.main()
Just as a warning I am at work and currently unable to test the code in this answer but it should at least point you in the right direction.
I assume the problem is that you can’t combine the two callbacks.
There are two solutions I can think of.
1) Use a global to store the clipboard data and read from it the keybinder callback
2) Get rid of the first callback.