-1

I need to copy the currently selected text from the currently active window in the currently active app. If the app support guiInfo.hwndCaret, then I use this. However, in some cases, the app does not support guiInfo.hwnd_Caret, and I have to fall back to plain copying to clipboard to get the currently selected text.

I am checking the WM_DRAWCLIPBOARD message to see when the content of the clipboard has changed.

However, when nothing is selected in the current active window, this message is not called because the clipboard has not changed.

How would I know that the Ctrl + C has been processed anyways?

Thank you!

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 1
    Nothing works "immediately". Copy+Paste depends on the applications involved. If you need to use apps that you don't own you should use UI Automation, not hack others' windows, for example: https://stackoverflow.com/a/4734275/403671 https://devblogs.microsoft.com/oldnewthing/20130408-00/?p=4733 https://devblogs.microsoft.com/oldnewthing/20150216-00/?p=44673 – Simon Mourier Apr 22 '22 at 05:58
  • 1
    In all likeliness, this is an example of the [XY Problem](https://xyproblem.info). You should ask about the actual problem you are trying to solve, not your proposed solution to an undisclosed problem. If you need to copy text into the clipboard then a delay of 25ms is inconsequential (though it's probably your profiling that's wrong here). The clipboard is a user-controlled resource. – IInspectable Apr 22 '22 at 06:18
  • Why would you care if the key was processed but did nothing? – Anders Apr 22 '22 at 12:31

1 Answers1

2

From MSDN

Each time the contents of the clipboard change, a 32-bit value known as the clipboard sequence number is incremented. A program can retrieve the current clipboard sequence number by calling the GetClipboardSequenceNumber function. By comparing the value returned against a value returned by a previous call to GetClipboardSequenceNumber, a program can determine whether the clipboard contents have changed.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Thanks. I would need to know that my Ctrl + C command has been processed. Unfortunately, WM_DRAWCLIPBOARD is only fired when the clipboard has actually changed. If the user did not select anything, this event is not fired. How could I get a notification anyways when my command has been processed? I have edited my post accordingly. – tmighty Apr 22 '22 at 12:08
  • "*How could I get a notification anyways when my command has been processed?*" There is no notification. You will likely need to use a global message hook via `SetWindowsHookEx()` to see if any windows receive `WM_KEY(DOWN|UP)` messages for your `Ctrl+C`, and/or a `WM_COPY` message, within a short period of time after you have issued your command. Otherwise, you would just have to use the lack of a `WM_DRAWCLIPBOARD` message or `GetClipboardSequenceNumber()` change within a given time period to mean that your command was processed but did nothing – Remy Lebeau Apr 22 '22 at 15:36
  • 2
    @tmi Consider using the actual solution to your actual problem: UI Automation. – IInspectable Apr 22 '22 at 19:31
  • UI Automation unfortunately does not return the selected text. I have tried it using Inspector.exe from Microsoft. – tmighty Apr 23 '22 at 19:10
  • 1
    @tmi [`IUIAutomationTextPattern::GetSelection`](https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomationtextpattern-getselection). – IInspectable Apr 25 '22 at 08:39