Reputation: 23
I want to write an application which pastes some text to the active window on some keystroke. How can I do this with Python or C++?
I want to write an app which will work like a daemon and on some global keystroke paste some text to the current active application (text editor, browser, and jabber client). I think I will need to use some low-level X Window server API.
Upvotes: 1
Views: 1139
Reputation: 15824
Interacting between multiple applications interfaces can be tricky, so it may help to provide more information on specifically what you are trying to do.
Nonetheless, you have a few options if you want to use the clipboard to accomplish this. On Windows, the Windows API provides GetClipboardData and SetClipboardData. To use these functions from Python you would want to take advantage of win32com.
On Linux, you have two main options (that I know of) for interacting with the clipboard. PyGTK provides a gtk.Clipboard object. There is also a command line tool for setting the X "selection," XSel. You could interact with XSel using Python by means of os.popen or subprocess. See this guide for info on using gtk.Clipboard and xsel.
In terms of how you actually use the clipboard. One application might poll the clipboard every so often looking for changes.
If you want to get into real "enterprisey" architecture, you could use a message bus, like RabbitMQ, to communicate between the two applications.
Upvotes: 1
Reputation: 415
If you use Tkinter (a GUI library that works in Linux, Mac OS X, Windows, and everywhere), and make any widget (for example a text widget), the copy (Ctrl + C) and paste (Ctrl + V) commands automatically work. For example, the following code shows a Text widget, where you can type multi-line text, and copy and paste to other applications, or from other application (for example, OpenOffice).
from Tkinter import *
root = Tk() # Initialize GUI
t = Text(root) # Create a text widget
t.grid() # Show the widget
root.mainloop() # Start the GUI
I have tested the code with Windows and Linux/KDE 3.5.
Upvotes: 0