fix: harden Windows clipboard persistence and reject paste-conflict hotkeys#28
Closed
ShunmeiCho wants to merge 1 commit intomainfrom
Closed
fix: harden Windows clipboard persistence and reject paste-conflict hotkeys#28ShunmeiCho wants to merge 1 commit intomainfrom
ShunmeiCho wants to merge 1 commit intomainfrom
Conversation
…otkeys (#27) Two bugs on Windows prevented the hotkey paste flow from working: Bug 1 — clipboard data vanished after `windowsSetClipboardText`. The short-lived PowerShell process that called `Set-Clipboard` owned the invisible clipboard window; when it exited, Windows destroyed the window and emptied the clipboard before `windowsSendCtrlShiftV` could fire. Replace `Set-Clipboard` and `Clipboard.SetImage` with `Clipboard.SetDataObject(..., $true)` (WinForms' explicit "persist after exit" contract) and additionally call `OleFlushClipboard()` as a belt-and-braces commit for delayed-render formats. Needs Windows smoke verification by the reporter; the fix adopts persistence semantics rather than claiming a definitive root cause. Bug 2 — configuring the hotkey to `ctrl+shift+v` made paste silently fail. `windowsSendCtrlShiftV` synthesizes the same keystroke, which is re-caught by our own `RegisterHotKey` loop (the `hotkeyRunning` guard swallows it). Plain `ctrl+v` would also hijack the system paste shortcut. Reject both combinations in `parseHotkey`, with error messages that explain the conflict. Tests: table-driven coverage for accepted bindings, rejection of the two conflict combinations across casings/orderings, a non-V key sanity case, and end-to-end rejection via `saveHotkeyConfig`. Refs: #27
This was referenced Apr 21, 2026
Owner
Author
|
Closing in favor of a split approach to #27:
This PR combined both portions in a single commit and description, which would confuse reviewers once the hotkey half was already in main. #30 has a clean diff of just Nothing is lost; no force-push was used. The branch behind this PR ( |
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.
Closes #27 once verified on a Windows host.
Summary
Two bugs on Windows prevented the hotkey paste flow from working. This PR addresses both.
Bug 2 (confirmed deterministic fix)
Configuring the hotkey to
ctrl+shift+vmade paste silently fail:windowsSendCtrlShiftVsynthesizesCtrl+Shift+VviaSendKeys.RegisterHotKeyloop, and thehotkeyRunningre-entry guard swallows it.ctrl+vwould also hijack the system paste shortcut.parseHotkeynow rejectsctrl+vandctrl+shift+vwith explanatory error messages (regardless of casing or modifier order), andsaveHotkeyConfiginherits the validation. The defaultalt+shift+vis unaffected.Bug 1 (persistence hardening, needs Windows smoke verification)
Clipboard data vanished between
windowsSetClipboardTextandwindowsSendCtrlShiftV:Set-Clipboard/Clipboard.SetTextgive clipboard ownership to an internal window inside the short-lived PowerShell process.The fix adopts explicit persistence semantics rather than asserting a single root cause:
Clipboard.SetDataObject(data, $true)— WinForms' documented "leave the data on the clipboard after this app exits" contract.OleFlushClipboard()as a belt-and-braces commit, because the exact persistence path for delayed-render formats (notablyBitmap) depends on Windows version and format. Using both keeps the fix robust without relying on any single API.The same treatment is applied to
windowsSetClipboardImage(used for the optional--restore-clipboardflow).Tests
Added table-driven tests in
cmd/cc-clip/hotkey_windows_test.go:TestParseHotkeyAccepts— default, casing, extra modifiers, function keysTestParseHotkeyRejectsPasteConflicts— ctrl+v / ctrl+shift+v across casings and modifier orderingsTestParseHotkeyNonVKeyNotRejected— scope check: the rule must only target the V keyTestSaveHotkeyConfigRejectsPasteConflicts— end-to-end rejection at the config layerVerification performed locally
make test— full suite passes on darwinmake vet— cleanGOOS=windows GOARCH=amd64 go build ./cmd/cc-clip— passesGOOS=windows GOARCH=arm64 go build ./cmd/cc-clip— passesGOOS=windows GOARCH=386 go build ./cmd/cc-clip— passesGOOS=windows GOARCH=amd64 go vet ./cmd/cc-clip— cleanGOOS=windows GOARCH=amd64 go test -c ./cmd/cc-clip— Windows-tagged tests compile cleanlyVerification still needed (Windows host)
I do not have a Windows host, so the runtime behavior of the clipboard persistence change must be confirmed by someone who does. Requested smoke checks:
cc-clip send --pasteon Windows 11 with Git Bash / Windows Terminal: image uploads and remote path is actually pasted into the terminalcc-clip hotkey --run-loopwith defaultalt+shift+v: copy image → hotkey → path pastedctrl+shift+vvia the config path: rejected with a clear error messagectrl+v: rejected with a clear error message--restore-clipboard, the original image is back on the clipboard after paste@nucleoid — would you be willing to test this branch on your Windows 11 setup? Binaries can be built from this branch, or I can attach prebuilt
.exeartifacts if you prefer.Files touched
cmd/cc-clip/hotkey_windows.go— conflict validation inparseHotkeycmd/cc-clip/hotkey_windows_test.go— new testscmd/cc-clip/send_windows.go—SetDataObject(..., $true)+OleFlushClipboardwrapper, applied to both text and image settersRefs #27.