Compare commits

...
Author SHA1 Message Date
agrynco ba61c2ee9b Run delayed selection regression independently
Refs #135
2026-11-26 12:33:15 +02:00
agrynco 1d60b3a0b6 Recover recent text selection in Claude and verify hosted editors
Refs #135

Refs #137
2026-11-26 12:30:37 +02:00
5 changed files with 100 additions and 27 deletions

No files matched your search

+8 -11
View File
@@ -660,21 +660,18 @@ class Transaction
}
auto text = select(count) ? copy() : std::wstring{};
diagnosticEvent(L"Selection initial match=" + std::to_wstring(matches(text)) + L" copied=" + diagnosticText(text));
// Chromium editors can apply only part of a burst of Shift+Left events.
// Extend only a verified suffix; never replace a partial or unrelated range.
// The editor may finish applying queued Shift+Left events after copying
// the suffix. Collapse to its right edge before trying the full range again.
if (valid() && !matches(text) && !text.empty() &&
(suffixMatches ? suffixMatches(text) : newlines(wordCandidate).ends_with(newlines(text))) &&
caretSteps(text) < count)
{
const auto missing = count - caretSteps(text);
diagnosticEvent(L"Selection: extending verified suffix by " + std::to_wstring(missing));
for (size_t i = 0; i < missing; ++i)
{
if (!valid() || !chord(VK_LEFT, MOD_SHIFT))
return {};
diagnosticEvent(L"Selection recovery Shift+Left sent=" + std::to_wstring(i + 1) + L" total=" + std::to_wstring(missing));
Sleep(25);
}
diagnosticEvent(L"Selection: reselecting full range after verified suffix");
if (!finishSelection(false))
return {};
Sleep(25);
if (!selectPrevious(count, [&] { return valid(); }, true))
return {};
text = copy();
diagnosticEvent(L"Selection: suffix recovery match=" + std::to_wstring(matches(text)) + L" copied=" + diagnosticText(text));
}
+50 -11
View File
@@ -10,6 +10,42 @@ class PasteProbe
IUIAutomation2 *automation{};
IUIAutomationElement *element{};
IUIAutomationTextPattern *pattern{};
HWND targetWindow{};
bool focusBelongsToTarget() const
{
DWORD process{};
GetWindowThreadProcessId(targetWindow, &process);
int focusedProcess{};
if (process && SUCCEEDED(element->get_CurrentProcessId(&focusedProcess)) &&
process == static_cast<DWORD>(focusedProcess))
return true;
// WebView2 can report a renderer process for the focused element.
// Require a native window in its UIA ancestry rooted in the target.
auto release = [](auto *value) { if (value) value->Release(); };
IUIAutomationTreeWalker *rawWalker{};
const auto walkerResult = automation->get_RawViewWalker(&rawWalker);
std::unique_ptr<IUIAutomationTreeWalker, decltype(release)> walker(rawWalker, release);
if (FAILED(walkerResult) || !walker) return false;
element->AddRef();
std::unique_ptr<IUIAutomationElement, decltype(release)> current(element, release);
for (int depth = 0; depth < 32 && current; ++depth)
{
UIA_HWND native{};
if (SUCCEEDED(current->get_CurrentNativeWindowHandle(&native)) && native)
{
const auto window = reinterpret_cast<HWND>(native);
return window == targetWindow || IsChild(targetWindow, window);
}
IUIAutomationElement *parent{};
const auto parentResult = walker->GetParentElement(current.get(), &parent);
current.reset(parent);
if (FAILED(parentResult)) break;
}
return false;
}
static std::optional<std::wstring> rangeText(IUIAutomationTextRange *range)
{
@@ -24,7 +60,7 @@ class PasteProbe
public:
std::wstring status = L"not-requested";
std::optional<std::wstring> precedingCharacter;
PasteProbe(bool enabled, HWND target)
PasteProbe(bool enabled, HWND target) : targetWindow(target)
{
if (!enabled)
return;
@@ -36,17 +72,15 @@ class PasteProbe
automation->put_TransactionTimeout(200);
if (FAILED(automation->GetFocusedElement(&element)) || !element)
return;
if (target)
if (target && GetForegroundWindow() != target)
{
DWORD process{};
GetWindowThreadProcessId(target, &process);
int focusedProcess{};
if (!process || FAILED(element->get_CurrentProcessId(&focusedProcess)) ||
process != static_cast<DWORD>(focusedProcess) || GetForegroundWindow() != target)
{
status = L"focused-control-changed";
return;
}
status = L"focused-window-changed";
return;
}
if (target && !focusBelongsToTarget())
{
status = L"focused-control-outside-target";
return;
}
BOOL password = TRUE;
if (FAILED(element->get_CurrentIsPassword(&password)) || password)
@@ -95,6 +129,11 @@ class PasteProbe
{
if (!pattern)
return {};
if (targetWindow && GetForegroundWindow() != targetWindow)
{
status = L"focused-window-changed";
return {};
}
if (characters <= 0 || characters > 4096)
{
status = L"readback-size-limit";
+6 -4
View File
@@ -676,9 +676,10 @@ bool needsSelectionDelay(HWND target)
{
const auto name = lower(processName(target));
return name == L"ms-teams" || name == L"msedgewebview2" ||
name == L"chrome" || name == L"msedge" || name == L"chatgpt" || name == L"codex";
name == L"chrome" || name == L"msedge" || name == L"chatgpt" ||
name == L"codex" || name == L"claude";
}
bool selectPrevious(size_t count, const std::function<bool()> &valid)
bool selectPrevious(size_t count, const std::function<bool()> &valid, bool forcePacing)
{
if (count > 100000)
return false;
@@ -686,9 +687,10 @@ bool selectPrevious(size_t count, const std::function<bool()> &valid)
// Shift+Left events can all use the same caret position and select only one
// character. Give each change time to settle before sending the next one.
const auto target = GetForegroundWindow();
const bool paced = forcePacing || needsSelectionDelay(target);
diagnosticEvent(L"SelectPrevious requested=" + std::to_wstring(count) +
L" mode=" + (needsSelectionDelay(target) ? std::wstring(L"paced-25ms") : std::wstring(L"batch")));
if (needsSelectionDelay(target))
(paced ? L" mode=paced-25ms" : L" mode=batch"));
if (paced)
{
for (size_t i = 0; i < count; ++i)
{
+1 -1
View File
@@ -75,7 +75,7 @@ ULONG_PTR inputMarker();
bool ownKeyboardInput(const KBDLLHOOKSTRUCT &);
INPUT keyboardInput(WORD key, DWORD flags = 0);
bool needsSelectionDelay(HWND);
bool selectPrevious(size_t count, const std::function<bool()> &valid);
bool selectPrevious(size_t count, const std::function<bool()> &valid, bool forcePacing = false);
bool modifiersReleased();
void releaseModifiers();
std::filesystem::path dataDirectory();
+35
View File
@@ -79,6 +79,7 @@ bool typeDuringMacros = false;
ULONGLONG macroStart{}, macroEnd{};
int copyFailuresRemaining = 0, copyRequests = 0;
int droppedSelectionKeys = 0;
int delayedSelectionKeys = 0;
bool trimCopiedSpaces = false;
int trimmedCopies = 0;
bool temporaryClipboardExcluded = true;
@@ -152,6 +153,16 @@ LRESULT CALLBACK observeEnter(HWND w, UINT message, WPARAM wp, LPARAM lp, UINT_P
--copyFailuresRemaining;
return 0;
}
if (delayedSelectionKeys)
{
DWORD start{}, end{};
SendMessageW(w, EM_GETSEL, reinterpret_cast<WPARAM>(&start), reinterpret_cast<LPARAM>(&end));
auto result = DefSubclassProc(w, message, wp, lp);
// Apply the remaining Shift+Left moves after publishing the partial selection.
SendMessageW(w, EM_SETSEL, end, start - std::min<DWORD>(start, delayedSelectionKeys));
delayedSelectionKeys = 0;
return result;
}
if (trimCopiedSpaces)
{
auto result = DefSubclassProc(w, message, wp, lp);
@@ -338,6 +349,8 @@ LRESULT CALLBACK procedure(HWND w, UINT msg, WPARAM wp, LPARAM lp)
switch (stage++)
{
case 0:
app->settings.writeLog = true;
ks::setLoggingEnabled(true);
saved = ks::readClipboard(app->window, 64 * 1024 * 1024);
captured = true;
app->ownSequence = ks::writeClipboard(app->window, ks::textClip(sentinel));
@@ -685,6 +698,26 @@ LRESULT CALLBACK procedure(HWND w, UINT msg, WPARAM wp, LPARAM lp)
case 59:
require(text() == L"OLDghbdtn NEXT", "hotkey without selection reconverts only the previous middle range");
require(ks::readClipboard(app->window).text() == sentinel, "selected and repeated conversions restore clipboard");
[[fallthrough]];
case 95:
if (!captured)
{
saved = ks::readClipboard(app->window, 64 * 1024 * 1024);
captured = true;
app->ownSequence = ks::writeClipboard(app->window, ks::textClip(sentinel));
}
prepare(L" nen NEXT", false);
SendMessageW(field, EM_SETSEL, 6, 6);
record(L"nen ");
droppedSelectionKeys = delayedSelectionKeys = 2;
app->transform(ks::Direct + ru);
stage = 96;
break;
case 96:
require(text() == L" " + ks::convertTo(L"nen ", app->layouts, ru, en) + L"NEXT",
"delayed selection preserves the older prefix and text after the caret");
require(!droppedSelectionKeys && !delayedSelectionKeys, "fixture applied the delayed selection moves");
require(ks::readClipboard(app->window).text() == sentinel, "delayed selection restores clipboard");
finish();
break;
case 66:
@@ -1392,6 +1425,8 @@ int main(int argc, char **argv)
stage = 93;
if (argc > 1 && std::string(argv[1]) == "--snippet-word-boundaries")
stage = 84;
if (argc > 1 && std::string(argv[1]) == "--delayed-selection-only")
stage = 95;
SetTimer(host, 1, 100, nullptr);
auto forwarding = SetWindowsHookExW(WH_KEYBOARD_LL,forwardingHook,GetModuleHandleW(nullptr),0);
require(forwarding!=nullptr,"downstream forwarding fixture installed before application hook");