user2788859
user2788859

Reputation: 67

accessing clipboard via win32clipboard

I am working on a project in which i have to continuouly check clipboard content. If clipboard content matches with certain specified data, then it should be deleted from clipboard. After doing a lot of googling, I find out that it can be done easily by win32clipboard api. I am using Python as a programming language. Following is a code for file(CF_HDROP) format:

import win32clipboard
import win32con

def filecopy():   
    try:
        win32clipboard.OpenClipboard()
        print win32clipboard.GetClipboardData(win32con.CF_HDROP)
        win32clipboard.CloseClipboard()
    except TypeError:
        pass

Following is a code for text format:

import win32clipboard
def textcopy():
    try:
        win32clipboard.OpenClipboard()
        data = win32clipboard.GetClipboardData()
        print data
        win32clipboard.CloseClipboard()
    except TypeError:
        pass

I am calling above functions in a infinite loop.

Individual function works correctly. But the problem with win32clipboard is that, after win32clipboard.OpenClipboard() command, win32clipboard lock the clipboard and only realise it after CloseClipboard() command. In between i cant copy anything in clipboard.

How can i solve this problem?? Any other suggestion are also welcome to achieve ultimate aim.

NOTE: Its not necessary to use python. You can use any other language or any other approach.

Upvotes: 1

Views: 2304

Answers (1)

Mark
Mark

Reputation: 108522

An infinite polling loop (especially one without delays) is going to be a problem since there's no way to read the contents without locking. Instead you should look into becoming a Clipboard viewer (pywin32 and msdn), that way you are notified of the clipboard contents change and then you can inspect it (get it and get out). If you google a bit on pywin32 and WM_DRAWCLIPBOARD, you'll find some python implementations.

Upvotes: 2

Related Questions