asweigart / pyperclip

Python module for cross-platform clipboard functions.
https://pypi.python.org/pypi/pyperclip
BSD 3-Clause "New" or "Revised" License
1.65k stars 196 forks source link

Fixed WSL Clipboard Handling #257

Closed vertliba closed 3 months ago

vertliba commented 6 months ago

This script fixes issues with copying and pasting non-ASCII characters in recent WSL Ubuntu releases.

Key Changes:

Base64 Encoding for Clipboard Operations: I have implemented Base64 encoding for text being copied to and pasted from the clipboard. This approach addresses the issue where special characters could be lost or misinterpreted when transferring data from Windows to Python and vice versa. By encoding the text to Base64 before passing it to Windows and decoding it back after retrieval, we ensure the integrity of the text data, including any special characters.

Implementation Details:

The copy_wsl function now encodes the text to Base64 before creating a PowerShell script to set the clipboard content. This script decodes the Base64 back to text within PowerShell, ensuring that the text placed on the clipboard is exactly as intended.

Similarly, the paste_wsl function retrieves the clipboard content as Base64 encoded text, which is then decoded in Python back to the original text.

Fixes issue https://github.com/asweigart/pyperclip/issues/244

oallauddin commented 4 months ago

@asweigart Can you review this fix? Having this issue in WSL2.

oxillix commented 4 months ago

@asweigart Please review :)

asweigart commented 3 months ago

Tested and it works for me. Thanks!