-
Notifications
You must be signed in to change notification settings - Fork 9k
Add support for OSC 52 clipboard copy in conhost #18949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| #include "srvinit.h" | ||
|
|
||
| #include "../interactivity/inc/ServiceLocator.hpp" | ||
| #include "../interactivity/win32/CustomWindowMessages.h" | ||
| #include "../types/inc/convert.hpp" | ||
|
|
||
| using Microsoft::Console::Interactivity::ServiceLocator; | ||
|
|
@@ -179,6 +180,32 @@ void CONSOLE_INFORMATION::SetBracketedPasteMode(const bool enabled) noexcept | |
| _bracketedPasteMode = enabled; | ||
| } | ||
|
|
||
| void CONSOLE_INFORMATION::CopyTextToClipboard(const std::wstring_view text) | ||
| { | ||
| const auto window = ServiceLocator::LocateConsoleWindow(); | ||
| if (window) | ||
| { | ||
| // The clipboard can only be updated from the main GUI thread, so we | ||
| // need to post a message to trigger the actual copy operation. But if | ||
| // the pending clipboard content is already set, a message would have | ||
| // already been posted, so there's no need to post another one. | ||
| const auto clipboardMessageSent = _pendingClipboardText.has_value(); | ||
| _pendingClipboardText = text; | ||
| if (!clipboardMessageSent) | ||
| { | ||
| PostMessageW(window->GetWindowHandle(), CM_UPDATE_CLIPBOARD, 0, 0); | ||
| } | ||
|
Comment on lines
+188
to
+197
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if I've just misunderstood how you're supposed to use the windows clipboard, but I couldn't get it working from a background thread, and this technique worked for me. I don't know if maybe there's a better way to do this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, reading doesn't require a HWND but writing does, to my knowledge. |
||
| } | ||
| } | ||
|
|
||
| std::optional<std::wstring> CONSOLE_INFORMATION::UsePendingClipboardText() | ||
| { | ||
| // Once the pending text has been used, we clear the variable to let the | ||
| // CopyTextToClipboard method know that the last CM_UPDATE_CLIPBOARD message | ||
| // has been processed, and future updates will require another message. | ||
| return std::exchange(_pendingClipboardText, {}); | ||
| } | ||
|
|
||
| // Method Description: | ||
| // - Return the active screen buffer of the console. | ||
| // Arguments: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,22 @@ using namespace Microsoft::Console::Types; | |
|
|
||
| #pragma region Public Methods | ||
|
|
||
| void Clipboard::CopyText(const std::wstring& text) | ||
| { | ||
| const auto clipboard = _openClipboard(ServiceLocator::LocateConsoleWindow()->GetWindowHandle()); | ||
| if (!clipboard) | ||
| { | ||
| LOG_LAST_ERROR(); | ||
| return; | ||
| } | ||
|
|
||
| EmptyClipboard(); | ||
| // As per: https://learn.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats | ||
| // CF_UNICODETEXT: [...] A null character signals the end of the data. | ||
| // --> We add +1 to the length. This works because .c_str() is null-terminated. | ||
| _copyToClipboard(CF_UNICODETEXT, text.c_str(), (text.size() + 1) * sizeof(wchar_t)); | ||
| } | ||
|
Comment on lines
+27
to
+41
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The content of this method is copied directly from |
||
|
|
||
| // Arguments: | ||
| // - fAlsoCopyFormatting - Place colored HTML & RTF text onto the clipboard as well as the usual plain text. | ||
| // Return Value: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,6 @@ | |
| #define CM_SET_KEYBOARD_LAYOUT (WM_USER+19) | ||
| #endif | ||
|
|
||
| #define CM_UPDATE_CLIPBOARD (WM_USER+20) | ||
|
|
||
| // clang-format on | ||
Uh oh!
There was an error while loading. Please reload this page.