0

Context:
I have been tasked with implementing the functionality to be able to retrieve the currently selected text of the active window then pressing a registered HotKey combo. Hotkeys registered with user32.dll RegisterHotKey
This is used to mark a phone number that will later be forwarded to a phone that will call this number, these phone numbers can come from a variety of places, some of these being: in app, email, database.

Problem:
The problem arises when the Chrome browser comes into the picture, Chome does not respond to SendKeys or anything I've thrown at it.

Question:
Using C#, how can I retrieve the currently marked text from a Chrome window, or any browser in general, as it needs to be compatible with whichever browser that the user prefers to use?

I've tried:
So far I've tried a lot of various methods and hacks, I've tried user32.dll sendmessage, SendKeys, AutomationElement among other cheap hacks that has long since been overwritten as it was just throwing errors.
My currently working code is grabbed from Github. This works for the most part when using notepad++ among other things. Do note that this does not work with discord either.

Simon Jensen
  • 488
  • 1
  • 3
  • 19
  • What's wrong with the user just using the clipboard – David Heffernan Feb 07 '20 at 09:48
  • They need to mark the text, press the hotkey and the phone should ring up the number. That's the boiled down version of the task that I've been given – Simon Jensen Feb 07 '20 at 09:50
  • This all breaks down in any program that doesn't use "standard" controls to handle text. It probably also won't work in Slack/Discord or any non-WPF/WinForms application. – Longoon12000 Feb 07 '20 at 10:07
  • Can't you make it so the user has to press CTRL+C before the call hotkey and it will just call whatever number is in the clipboard? Like CTRL+C,K or something. That would universally work with any application that supports Copy/Paste. – Longoon12000 Feb 07 '20 at 10:09
  • I asked this question and they scuffed at the idea. Agreeing that it is a workaround, but they prefer to have that part automatic. I haven't found any global way to just tell the system to perform a Ctrl+C without it needing the window handle of the currently active window. I know its possible to get the selected text cause many translator programs are able to get marked text from any application, including chrome. – Simon Jensen Feb 07 '20 at 10:37
  • 1
    The problem you will have is that you will have to use different code for different applications, in order to extract the text, and there will exist apps for which you simply won't be able to extract the text. At some point the people demanding that you achieve this will have to face up to reality. – David Heffernan Feb 07 '20 at 10:59
  • The technology you are looking for is called [UI Automation](https://learn.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview). If that fails with Google Chrome, file a defect report with the vendor. Make sure to check both UI Automation as well as MSAA. – IInspectable Feb 07 '20 at 11:13
  • It's impossible in the general case. Some apps just don't want you to capture what they expose. If UI automation doesn't work (Chrome for example lets you see toolbars, controls, status, but not the pages content, so it's clearly a design choice), then take a screenshot, run OCR (and pray). Even though, some apps won't let you do this: https://stackoverflow.com/a/56713048/403671 – Simon Mourier Feb 07 '20 at 11:20

0 Answers0