4

I'm doing a universal text auto completer application in C#. So I need to get the caret position in the currently active window (it may be Notepad, Ms Word etc). What should I do to get the X,Y coordinates of the caret? I need it to display suggestions under the cursor (caret) while the user is typing.

My program is running in the background. It should detect the presence of caret in whatever window currently active and get the real time location of caret. Location value should update whenever the user enter text.

I don't know how to do all these. Is this all possible using C#? Or should I change the programming language?

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
Dreamist
  • 123
  • 8
  • I see different problems here: 1. [Get active application](http://stackoverflow.com/questions/2483537/is-it-possible-to-get-the-name-of-current-active-application) 2. [Get cursor coordinates](http://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp) I hope this helps you to get closer to your goal. – Alina B. Mar 10 '13 at 10:48

1 Answers1

1

Language choice is largely irrelevant for this problem. Text input is difficult in itself.

Solving your problem is going to be difficult because applications often use custom or heavily customized controls to edit text. Notepad uses the standard Edit control, so you can use the EM_GETSEL message to retrieve caret position in characters and EM_POSFROMCHAR to convert it to a screen location. Similar messages exist for the standard Rich Edit control. However, with Microsoft Word you will probably have to use COM to call into it and find out the caret position. Other applications will have other APIs, which you will have to research, reverse-engineer, support and test. And this is just the beginning — I expect you will want to retrieve text around the caret and modify it, and/or display a UI to provide choices for the user. It's going to be a major PITA, so I'd look into IME APIs to see if there is anything suitable there. There should be, since East Asian input modes are a sort of auto completer in themselves. IME's advantage is that it does not depend on the target control.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56