Skip to content

Handle Ctrl+V and Shift+Insert paste from the clipboard#375

Closed
shibayan wants to merge 1 commit into
masterfrom
fix/clipboard-paste
Closed

Handle Ctrl+V and Shift+Insert paste from the clipboard#375
shibayan wants to merge 1 commit into
masterfrom
fix/clipboard-paste

Conversation

@shibayan

@shibayan shibayan commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Summary

Since v2.3.3, enabling TreatControlCAsInput disables the legacy Windows console's built-in Ctrl+V paste handling, so pressing Ctrl+V delivers a raw ^V input record to the prompt, which just beeps (Windows Terminal is unaffected because it handles paste itself — which is why this was hard to reproduce).

FormBase now binds Ctrl+V and Shift+Insert to a paste handler that reads the clipboard (Win32 CF_UNICODETEXT) and routes each character through HandleTextInput, 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.ClipboardTextProvider so it can be replaced by consumers and stubbed in tests. On non-Windows platforms the default provider returns null (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

  • 4 new form interaction tests: Ctrl+V into InputForm, control-character stripping, empty-clipboard fallback, Shift+Insert into SelectForm filtering
  • Win32 clipboard read verified on a real Windows machine (Set-ClipboardClipboardHelper.GetText() round-trip)
  • Full suite: 328 passed, dotnet format clean

🤖 Generated with Claude Code

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>
@shibayan shibayan self-assigned this Jul 5, 2026
@shibayan shibayan added the bug Something isn't working label Jul 5, 2026
@shibayan shibayan requested a review from Copilot July 5, 2026 11:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ClipboardTextProvider to allow consumers/tests to replace clipboard access.
  • Add interaction tests covering paste into InputForm and SelectForm filtering, 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.

@shibayan shibayan closed this Jul 5, 2026
@shibayan shibayan deleted the fix/clipboard-paste branch July 5, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ctrl + V (insert) does not work anymore since 2.3.3

2 participants