|
1 | 1 | # noinspection PyPackageRequirements |
2 | | -import wx |
| 2 | +import pyclip |
3 | 3 | from logbook import Logger |
4 | 4 |
|
5 | 5 | logger = Logger(__name__) |
6 | 6 |
|
7 | 7 |
|
8 | 8 | def toClipboard(text): |
9 | | - """ |
10 | | - Copy text to clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY. |
11 | | -
|
12 | | - On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections, |
13 | | - causing "already open" errors. This function ensures we always use CLIPBOARD. |
14 | | -
|
15 | | - See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361 |
16 | | - """ |
17 | | - clipboard = wx.TheClipboard |
18 | | - try: |
19 | | - # Explicitly use CLIPBOARD selection, not PRIMARY selection |
20 | | - # This prevents X11 confusion between the two clipboard types |
21 | | - clipboard.UsePrimarySelection(False) |
22 | | - |
23 | | - if clipboard.Open(): |
24 | | - try: |
25 | | - data = wx.TextDataObject(text) |
26 | | - clipboard.SetData(data) |
27 | | - return True |
28 | | - finally: |
29 | | - clipboard.Close() |
30 | | - else: |
31 | | - logger.debug("Failed to open clipboard for writing") |
32 | | - return False |
33 | | - except Exception as e: |
34 | | - logger.warning("Error writing to clipboard: {}", e) |
35 | | - return False |
| 9 | + pyclip.copy(text) |
36 | 10 |
|
37 | 11 |
|
38 | 12 | def fromClipboard(): |
39 | 13 | """ |
40 | | - Read text from clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY. |
41 | | -
|
42 | | - On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections, |
43 | | - causing "already open" errors. This function ensures we always use CLIPBOARD. |
44 | | -
|
45 | | - See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361 |
| 14 | + Read text from clipboard. Uses pyclip to grab in a cross-platform, reliable manner. |
46 | 15 | """ |
47 | | - clipboard = wx.TheClipboard |
48 | | - try: |
49 | | - # Explicitly use CLIPBOARD selection, not PRIMARY selection |
50 | | - # This prevents X11 confusion between the two clipboard types |
51 | | - clipboard.UsePrimarySelection(False) |
52 | | - |
53 | | - if clipboard.Open(): |
54 | | - try: |
55 | | - data = wx.TextDataObject() |
56 | | - if clipboard.GetData(data): |
57 | | - return data.GetText() |
58 | | - else: |
59 | | - logger.debug("Clipboard open but no CLIPBOARD data available") |
60 | | - return None |
61 | | - finally: |
62 | | - clipboard.Close() |
63 | | - else: |
64 | | - logger.debug("Failed to open clipboard for reading") |
65 | | - return None |
66 | | - except Exception as e: |
67 | | - logger.warning("Error reading from clipboard: {}", e) |
68 | | - return None |
| 16 | + data = pyclip.paste(text=True) |
| 17 | + if not isinstance(data, str): |
| 18 | + data = data.decode('utf-8') |
| 19 | + logger.debug("Pasted data: {}", data) |
| 20 | + return data |
0 commit comments