I have C# window application and I want to get text where the cursor is currently located or text is selected from the other application like notepad, notepad++ or any browser etc.
-
It is not possible to do so in general. It might be possible to do it for a specific application, but something that will work globally for all applications will be very hard. What is yojr use case can't you use copy/paste? – Eli Algranti Jan 22 '14 at 09:44
-
@EliAlgranti - This is possible because Its functionality already implemented in "WordWeb" Hotkey. I'm able to get selected text using copy/paste but I can't get text from the cursor position. – Ashish Jan 22 '14 at 09:58
-
My use case is like I want a text from external application on press HotKey. – Ashish Jan 22 '14 at 10:09
-
1Sorry you are correct was thinking about something else. You need to hook your program to receive windows messages (see http://stackoverflow.com/questions/11361811/capture-all-windows-messages) and when your hot key is called get hold of the current window and get it to send you the selected text. You might need hacks to support certain applications as not all applications want to play ball. See http://stackoverflow.com/questions/2251578/how-do-i-get-the-selected-text-from-the-focused-window-using-native-win32-api. – Eli Algranti Jan 22 '14 at 22:46
-
1[RangeFromPoint](http://msdn.microsoft.com/en-us/library/system.windows.automation.textpattern.rangefrompoint(v=vs.110).aspx) will get you the text at the cursor. Expand it to the word at the cursor with `ExpandToEnlosingUnit(Word)`. – Raymond Chen Jan 23 '14 at 15:00
-
Could you just use Copy and then monitor the clipboard? Then your just doing CTRL+C on all the stuff you want to scrape? You can process the contents from your app on a background thread. – Matt Jan 30 '14 at 20:42
7 Answers
Did you already have a look at this CodeProject article ? This could be a start even if this is not exactly what you are looking for.
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms632604(v=vs.85).aspx.
If it do not solve your issue, have a look at http://msdn.microsoft.com/en-us/library/system.windows.automation.textpattern.getselection(v=vs.110).aspx, as suggested in the comments.

- 1,929
- 15
- 28
Getting the text under the cursor (or from the caret) requires UI Automation and TextPattern support from the application. The problem is that not all applications support this, and the older the application, the less likely it is to have TextPattern support.
Getting selected text is, ironically enough, somewhat easier, although still not 100%. I outlined a solution in this answer. It does involve managing focus and manipulating the clipboard for the most general solution, and it is by no means perfect.
Another option, that involves a ton of work, is to use a mirror driver to capture the screen contents, and then use other technologies (OCR, etc.) to capture the text. I don't really recommend this; it's not supported in Windows 8 and above, but if you absolutely have to have 100% support across applications with the least impact, then it's a possibility. It's a lot of work, though. Definitely not for the squeamish.

- 1
- 1

- 13,774
- 7
- 30
- 71
This is possible using Accessibility technologies (like screen readers). However, it will require a great deal of troubleshooting:
The answer about MSAA on the following question is where you will need to start.
Best way to get the 'word before the cursor' in any open app's text field
Also, the following question is helpful about implementing it:
The problem is that you are trying to get data from another application. Unless that application supports a way to provide this to you it will be very difficult.
It would be much easier if the info could be retrieved from within the application, like from within a textbox or rich text control on a form

- 271
- 1
- 8
You can use clipborad to copy or get that text and then transfer it to your desired window.

- 755
- 1
- 14
- 34
You can use SendKeys
class to demonstrate the keyboard.
For example you can use SendKeys.Send("^C")
in your program and then a code to focus on Notepad++ and then SendKeys.Send("^V")
.
SendKeys.Send("^C");
// code to change active window and focus on Notepad++.
SendKeys.Send("^V");

- 8,806
- 12
- 64
- 111
Thanks for help me.
Still I'm not able to get the text from the caret position. So finally I get the all the text from the active window and fetch my text using Regex.
private string SelectText(IntPtr hWnd)
{
string text = string.Empty;
Regex regex = new Regex(@"(\d{3}-\w{5,8})");
if (InputSimulator.IsKeyDown(VirtualKeyCode.SHIFT))
{
InputSimulator.SimulateKeyUp(VirtualKeyCode.SHIFT);
}
if (InputSimulator.IsKeyDown(VirtualKeyCode.MENU))
{
InputSimulator.SimulateKeyUp(VirtualKeyCode.MENU);
}
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text) && regex.IsMatch(text))
{
Thread.Sleep(100);
text.Trim();
string[] textArr = text.Split(' ');
text = textArr[textArr.Length - 1];
}
else
{
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_A);
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
ClickOnPoint();
Thread.Sleep(100);
text = Clipboard.GetText();
MatchCollection matchCollection = regex.Matches(text);
if (matchCollection.Count > 0)
{
text = matchCollection[0].Value;
}
else
{
text = string.Empty;
}
}
Clipboard.Clear();
return text;
}

- 123
- 2
- 7