Monitoring the windows clipboard

David Bolen db3l at fitlinxx.com
Fri Dec 7 22:49:50 EST 2001


toflatpy2 at oaktown.org (toflat) writes:

> Hey there, newbie guy here again, with you guessed it, another newbie
> question...
> 
> I have this shareware app that works with text files and has an
> "Automatic paste" feature. When this feature is activated it
> automatically pastes any text that is copied to the windows clipboard
> to whatever file is active in the program. So it has a way of
> monitoring the windows clipboard and pasting the contents to a new
> line in the file whenever something new is added to the clipboard. It
> seems to be pretty instantaneous (i get a little system 'beep').
> 
> Is there a way I can get Python to do the same thing? 

There is a way for a program to register itself as a "clipboard
viewer" (with the SetClipboardViewer call) which requires a Window
(can be hidden) and message queue to process messages, one of which is
the WM_DRAWCLIPBOARD message that is sent whenever the clipboard
changes.

I'm not sure that there's any other way to get notified, but
presumably your shareware app could be doing this and then taking any
text data from the clipboard in response to the message, rather than
displaying it.

Presumably you could do something similar using the win32clipboard
module, which does wrap the necessary functions, along with win32gui
which you'd need to create a window and process the message loop.
When you receive the change notification you can use the functions in
win32clipboard to retrieve the data.

There's a clipboard demo in the demos directory beneath win32 that
gets installed with the win32all package - it only does direct access
to the clipboard (no monitoring) but might help with the latter part.
There are other demos in the pythonwin/pywin/demos subdirectory off of
your Python installation (also installed with win32all) that cover
some of the basic window operations.

I haven't written code interacting with the clipboard myself at this
point, but hopefully this can get you started in the right direction.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list