-
Notifications
You must be signed in to change notification settings - Fork 83
/
win32_clipboard.py
109 lines (93 loc) · 3.77 KB
/
win32_clipboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
#*****************************************************************************
# Copyright (C) 2003-2006 Jack Trainor.
# Copyright (C) 2006 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
###################################
#
# Based on recipe posted to ctypes-users
# see archive
# http://aspn.activestate.com/ASPN/Mail/Message/ctypes-users/1771866
#
#
###################################################################################
#
# The Python win32clipboard lib functions work well enough ... except that they
# can only cut and paste items from within one application, not across
# applications or processes.
#
# I've written a number of Python text filters I like to run on the contents of
# the clipboard so I need to call the Windows clipboard API with global memory
# for my filters to work properly.
#
# Here's some sample code solving this problem using ctypes.
#
# This is my first work with ctypes. It's powerful stuff, but passing arguments
# in and out of functions is tricky. More sample code would have been helpful,
# hence this contribution.
#
###################################################################################
from __future__ import print_function, unicode_literals, absolute_import
from ctypes import *
from pyreadline.keysyms.winconstants import CF_UNICODETEXT, GHND
from pyreadline.unicode_helper import ensure_unicode,ensure_str
OpenClipboard = windll.user32.OpenClipboard
OpenClipboard.argtypes = [c_int]
EmptyClipboard = windll.user32.EmptyClipboard
GetClipboardData = windll.user32.GetClipboardData
GetClipboardData.argtypes = [c_int]
GetClipboardFormatName = windll.user32.GetClipboardFormatNameA
GetClipboardFormatName.argtypes = [c_uint,c_char_p,c_int]
SetClipboardData = windll.user32.SetClipboardData
SetClipboardData.argtypes = [c_int,c_int]
EnumClipboardFormats = windll.user32.EnumClipboardFormats
EnumClipboardFormats.argtypes = [c_int]
CloseClipboard = windll.user32.CloseClipboard
CloseClipboard.argtypes = []
GlobalAlloc = windll.kernel32.GlobalAlloc
GlobalLock = windll.kernel32.GlobalLock
GlobalLock.argtypes = [c_int]
GlobalUnlock = windll.kernel32.GlobalUnlock
GlobalUnlock.argtypes = [c_int]
memcpy = cdll.msvcrt.memcpy
def enum():
OpenClipboard(0)
q = EnumClipboardFormats(0)
while q:
q = EnumClipboardFormats(q)
CloseClipboard()
def getformatname(format):
buffer = c_buffer(" "*100)
bufferSize = sizeof(buffer)
OpenClipboard(0)
GetClipboardFormatName(format, buffer, bufferSize)
CloseClipboard()
return buffer.value
def GetClipboardText():
text = ""
if OpenClipboard(0):
hClipMem = GetClipboardData(CF_UNICODETEXT)
if hClipMem:
GlobalLock.restype = c_wchar_p
text = GlobalLock(hClipMem)
GlobalUnlock(hClipMem)
CloseClipboard()
return ensure_unicode(text)
def SetClipboardText(text):
buffer = create_unicode_buffer(ensure_unicode(text))
bufferSize = sizeof(buffer)
hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize))
GlobalLock.restype = c_void_p
lpGlobalMem = GlobalLock(c_int(hGlobalMem))
memcpy(lpGlobalMem, addressof(buffer), c_int(bufferSize))
GlobalUnlock(c_int(hGlobalMem))
if OpenClipboard(0):
EmptyClipboard()
SetClipboardData(c_int(CF_UNICODETEXT), c_int(hGlobalMem))
CloseClipboard()
if __name__ == '__main__':
txt = GetClipboardText() # display last text clipped
print(txt)