views:

282

answers:

2

I want to write application which paste some text to active window on some keystroke. How can i do this with Python or C++ ?

Update: Sorry people. I think i don't describe problem clearly. I want to write app, which will work like a daemon and on some global keystroke paste some text to current active application (text editor, browser, jabber client) I think i will need to use some low level xserver api.

+1  A: 

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.

John Paulett
A: 

If you use Tkinter (a GUI library that works in Linux, MACOS, 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/KDE3.5

cyberthanasis