Skip to content

Make the Ctrl+C copy reliable, and say why it failed when it does - #6

Merged
sethdtwigg merged 1 commit into
masterfrom
fix/clipboard-copy-reliability
Sep 6, 2026
Merged

Make the Ctrl+C copy reliable, and say why it failed when it does#6
sethdtwigg merged 1 commit into
masterfrom
fix/clipboard-copy-reliability

Conversation

@sethdtwigg

Copy link
Copy Markdown
Owner

Selecting a reference in Notepad++ and pressing the hotkey logged Clipboard: No text found after copy operation and inserted nothing.

The real problem: one message, three causes

That single message was produced by three completely different failures, all of which returned an empty string:

  1. Modifier keys still physically held from the hotkey
  2. The clipboard could not be opened
  3. Nothing was actually selected

All three are now fixed, and — more importantly — are now told apart in the log.

1. Held modifiers (most likely cause here)

The copy is synthesised a fraction of a second after the hotkey fires, and nobody releases Ctrl+Alt+L that fast. A physically held Alt combines with the synthetic keystroke, so the target application receives Ctrl+Alt+C instead of Ctrl+C and copies nothing — indistinguishable from "no text was selected".

The timestamps in the report support this: Hotkey pressed! at 14:01:05.759, selection read at 14:01:06.321 — Ctrl+C went out roughly 100–200 ms after the hotkey.

SendKeys now releases any held Alt, Shift or Win before synthesising anything.

2. Clipboard contention

OpenClipboard was called once, and on failure returned an empty string indistinguishable from an empty clipboard. Clipboard managers, editors and browsers all hold the clipboard in short bursts, so this fails often enough to matter. It now retries for up to 500 ms, and reads report why they came back empty.

3. Guessing at a delay

A fixed Sleep(300) was both too short for slow applications and wasted time for fast ones, and told us nothing. The clipboard sequence number is now sampled before the copy and polled for up to 1.5 s. If it never changes, nothing was copied — and the log says exactly that rather than blaming the read:

Clipboard: Ctrl+C did not change the clipboard after 1500ms - was any text selected?

Log noise

Two expected fallback conditions were logged as ERROR on every successful run — Notepad++ is Scintilla-based and exposes no UIA TextPattern, so both fire every time:

ERROR: UI Automation: No text selection found
ERROR: Target window is not an edit control (class: Notepad++)

Both are now ordinary log lines. They also set m_last_error, which buried real failures.

Honest status

I could not reproduce the failure interactively to prove which of the three it was — GUI automation of "hold Alt while another app has focus" was unreliable in my environment, and I'm not going to claim a diagnosis I didn't verify. What I've done instead is make all three causes non-failures where possible, and unambiguous in the log where not. If it recurs, the log will now name the cause outright.

Harness 1053 checks and self-test 242 checks still pass. Version bumped to 1.1.1.

Reported: selecting a reference in Notepad++ and pressing the hotkey logged
"Clipboard: No text found after copy operation" and inserted nothing.

The log could not distinguish three different failures, all of which produced
that one message with an empty result:

  1. modifier keys still physically held from the hotkey,
  2. the clipboard could not be opened,
  3. nothing was actually selected.

All three are now addressed and, more importantly, are now distinguishable.

Held modifiers. The copy is synthesised a fraction of a second after the hotkey
fires, and nobody releases Ctrl+Alt+L that fast. A physically held Alt combines
with the synthetic keystroke, so the target sees Ctrl+Alt+C rather than Ctrl+C
and copies nothing - which looked exactly like "no text was selected". SendKeys
now releases any held Alt, Shift or Win first. This is the most likely cause of
the report: the timestamps show Ctrl+C going out roughly 100-200 ms after the
hotkey.

Clipboard contention. OpenClipboard was called once and, on failure, returned an
empty string indistinguishable from an empty clipboard. Clipboard managers,
editors and browsers all hold the clipboard in short bursts, so this fails often
enough to matter. It now retries for up to 500 ms, and reads report whether they
found no text or could not open the clipboard at all.

Guessing at a delay. A fixed 300 ms sleep was both too short for slow
applications and wasted time for fast ones, and told us nothing. The clipboard
sequence number is now sampled before the copy and polled for up to 1.5 s: if it
never changes, nothing was copied, and the log says so ("Ctrl+C did not change
the clipboard - was any text selected?") rather than blaming the read.

Also demoted two expected fallback conditions from ERROR to ordinary log lines.
"No text selection found" and "Target window is not an edit control" happen on
every successful clipboard fallback - Notepad++ is Scintilla-based and exposes
no UIA TextPattern - and logging them as errors set m_last_error and buried real
failures in noise.

Version 1.1.1.
@sethdtwigg
sethdtwigg merged commit 7659ac8 into master Sep 6, 2026
3 checks passed
@sethdtwigg
sethdtwigg deleted the fix/clipboard-copy-reliability branch September 6, 2026 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant