|
10 | 10 | if platform != 'win':
|
11 | 11 | raise SystemError('unsupported platform for Windows clipboard')
|
12 | 12 |
|
| 13 | +user32 = ctypes.windll.user32 |
| 14 | +kernel32 = ctypes.windll.kernel32 |
| 15 | +msvcrt = ctypes.cdll.msvcrt |
| 16 | +c_char_p = ctypes.c_char_p |
| 17 | + |
13 | 18 |
|
14 | 19 | class ClipboardWindows(ClipboardBase):
|
15 | 20 |
|
16 | 21 | def get(self, mimetype='text/plain'):
|
17 |
| - ctypes.windll.user32.OpenClipboard(0) |
| 22 | + user32.OpenClipboard(0) |
18 | 23 | # 1 is CF_TEXT
|
19 |
| - pcontents = ctypes.windll.user32.GetClipboardData(1) |
20 |
| - data = ctypes.c_char_p(pcontents).value |
| 24 | + pcontents = user32.GetClipboardData(1) |
| 25 | + data = c_char_p(pcontents).value |
21 | 26 | #ctypes.windll.kernel32.GlobalUnlock(pcontents)
|
22 |
| - ctypes.windll.user32.CloseClipboard() |
| 27 | + user32.CloseClipboard() |
23 | 28 | return data
|
24 | 29 |
|
25 | 30 | def put(self, data, mimetype='text/plain'):
|
26 | 31 | GMEM_DDESHARE = 0x2000
|
27 |
| - ctypes.windll.user32.OpenClipboard(0) |
28 |
| - ctypes.windll.user32.EmptyClipboard() |
| 32 | + user32.OpenClipboard(0) |
| 33 | + user32.EmptyClipboard() |
29 | 34 | try:
|
30 | 35 | # works on Python 2 (bytes() only takes one argument)
|
31 |
| - hCd = ctypes.windll.kernel32.GlobalAlloc( |
| 36 | + hCd = kernel32.GlobalAlloc( |
32 | 37 | GMEM_DDESHARE, len(bytes(text)) + 1)
|
33 | 38 | except TypeError:
|
34 | 39 | # works on Python 3 (bytes() requires an encoding)
|
35 |
| - hCd = ctypes.windll.kernel32.GlobalAlloc( |
| 40 | + hCd = kernel32.GlobalAlloc( |
36 | 41 | GMEM_DDESHARE, len(bytes(text, 'ascii')) + 1)
|
37 |
| - pchData = ctypes.windll.kernel32.GlobalLock(hCd) |
| 42 | + pchData = kernel32.GlobalLock(hCd) |
38 | 43 | try:
|
39 | 44 | # works on Python 2 (bytes() only takes one argument)
|
40 |
| - ctypes.cdll.msvcrt.strcpy( |
41 |
| - ctypes.c_char_p(pchData), bytes(text)) |
| 45 | + msvcrt.strcpy( |
| 46 | + c_char_p(pchData), bytes(text)) |
42 | 47 | except TypeError:
|
43 | 48 | # works on Python 3 (bytes() requires an encoding)
|
44 |
| - ctypes.cdll.msvcrt.strcpy( |
45 |
| - ctypes.c_char_p(pchData), bytes(text, 'ascii')) |
46 |
| - ctypes.windll.kernel32.GlobalUnlock(hCd) |
47 |
| - ctypes.windll.user32.SetClipboardData(1, hCd) |
48 |
| - ctypes.windll.user32.CloseClipboard() |
| 49 | + msvcrt.strcpy( |
| 50 | + c_char_p(pchData), bytes(text, 'ascii')) |
| 51 | + kernel32.GlobalUnlock(hCd) |
| 52 | + user32.SetClipboardData(1, hCd) |
| 53 | + user32.CloseClipboard() |
49 | 54 |
|
50 | 55 | def get_types(self):
|
51 | 56 | return list('text/plain',)
|
|
0 commit comments