Handle Ctrl+V and Shift+Insert paste from the clipboard#375
Closed
shibayan wants to merge 1 commit into
Closed
Conversation
Since v2.3.3, enabling TreatControlCAsInput disables the legacy Windows console's own Ctrl+V paste handling, so the key arrives as a raw ^V input record and prompts just beep. Bind Ctrl+V and Shift+Insert in FormBase to read the clipboard (Win32 CF_UNICODETEXT) and route each character through HandleTextInput, so text input, password input, and select filtering all accept pasted text. Control characters in the pasted text are stripped. The clipboard access is exposed as PromptConfiguration.ClipboardTextProvider so it can be replaced (or stubbed in tests). On non-Windows platforms the default provider returns null and paste continues to be handled by the terminal emulator itself. Fixes #237 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR restores paste behavior in Sharprompt forms when TreatControlCAsInput is enabled by handling Ctrl+V and Shift+Insert at the form layer and routing clipboard text through existing text input handling.
Changes:
- Add a form-level paste keybinding (Ctrl+V / Shift+Insert) that reads clipboard text and feeds it into
HandleTextInput, stripping control characters. - Introduce
PromptConfiguration.ClipboardTextProviderto allow consumers/tests to replace clipboard access. - Add interaction tests covering paste into
InputFormandSelectFormfiltering, including control-character stripping and empty-clipboard fallback.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Sharprompt.Tests/Forms/FormInteractionTests.cs | Adds tests validating paste behavior, stripping, and fallback behavior across forms. |
| src/Sharprompt/PromptConfiguration.cs | Exposes ClipboardTextProvider for configurable/stubbable clipboard access. |
| src/Sharprompt/Internal/NativeMethods.cs | Adds Win32 P/Invoke declarations/constants needed for clipboard access. |
| src/Sharprompt/Internal/ClipboardHelper.cs | Implements Windows CF_UNICODETEXT clipboard read with non-Windows returning null. |
| src/Sharprompt/Forms/FormBase.cs | Binds Ctrl+V / Shift+Insert and implements paste routing via HandleTextInput. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Since v2.3.3, enabling
TreatControlCAsInputdisables the legacy Windows console's built-in Ctrl+V paste handling, so pressing Ctrl+V delivers a raw^Vinput record to the prompt, which just beeps (Windows Terminal is unaffected because it handles paste itself — which is why this was hard to reproduce).FormBasenow binds Ctrl+V and Shift+Insert to a paste handler that reads the clipboard (Win32CF_UNICODETEXT) and routes each character throughHandleTextInput, so text input, password input, and select/multiselect filtering all accept pasted text. Control characters (e.g. newlines) are stripped.Clipboard access is exposed as
PromptConfiguration.ClipboardTextProviderso it can be replaced by consumers and stubbed in tests. On non-Windows platforms the default provider returnsnull(terminal emulators translate paste into regular key input there), and an empty/unavailable clipboard falls back to the existing beep behavior.Fixes #237
Test plan
InputForm, control-character stripping, empty-clipboard fallback, Shift+Insert intoSelectFormfilteringSet-Clipboard→ClipboardHelper.GetText()round-trip)dotnet formatclean🤖 Generated with Claude Code