Created
March 8, 2014 02:59
-
-
Save globby/9424649 to your computer and use it in GitHub Desktop.
A simple clipboard modification class with ctypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
class Clipboard: | |
def __init__(self): | |
self.strcpy = cdll.msvcrt.strcpy | |
self.OpenClipboard = windll.user32.OpenClipboard | |
self.CloseClipboard = windll.user32.CloseClipboard | |
self.EmptyClipboard = windll.user32.EmptyClipboard | |
self.GetClipboardData = windll.user32.GetClipboardData | |
self.SetClipboardData = windll.user32.SetClipboardData | |
self.GlobalAlloc = windll.kernel32.GlobalAlloc | |
self.GlobalLock = windll.kernel32.GlobalLock | |
self.GlobalUnlock = windll.kernel32.GlobalUnlock | |
self.GMEM_DDESHARE = 0x2000 | |
def getClipboard(self): | |
self.OpenClipboard(c_int(0)) | |
contents = c_char_p(self.GetClipboardData(c_int(1))).value | |
self.CloseClipboard() | |
return contents | |
def emptyClipboard(self): | |
self.OpenClipboard(c_int(0)) | |
self.EmptyClipboard() | |
self.CloseClipboard() | |
def setClipboard(self,data): | |
self.OpenClipboard(c_int(0)) | |
self.EmptyClipboard() | |
alloc = self.GlobalAlloc(self.GMEM_DDESHARE, len(bytes(data))+1) | |
lock = self.GlobalLock(alloc) | |
self.strcpy(c_char_p(lock),bytes(data)) | |
self.GlobalUnlock(alloc) | |
self.SetClipboardData(c_int(1),alloc) | |
self.CloseClipboard() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment