Read text from clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY. On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections, causing "already open" errors. This function ensures we always use CLIPBOARD. See: https://discuss.wxpython.org/t/wx-the
()
| 36 | |
| 37 | |
| 38 | def fromClipboard(): |
| 39 | """ |
| 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 |
| 46 | """ |
| 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 |
no test coverage detected