I’m writing a text entry app that sits in the System Tray and assists in text entry via keyboard hooks. The current character the user types is determined by previous letters.
So if you have “I want to ben|” where “|” represents the caret, when the user hits the keyboard the next letter is determined by doing calculation on the previous letters of the word, “ben”. It could either turn out to be ‘d’ > “bend” or ‘t’ > “bent”, etc.
This works fine if I’m starting each word from scratch, I can just keep track of the word in a String in my program.
The problem arises when I set the caret down in the middle of a word. I need to detect what characters come before the caret position. The situation also occurs if I press the arrow keys, etc.
In a past AutoHotkey app I did this by sending a “Shift + Ctrl + Left Arrow” command to highlight the characters, a “Ctrl + C” command to copy it, and then saved the clipboard into my String. I’m hoping that in a more robust language like C# I can avoid the ugly highlighting artifacts of this technique. Is there a way to get the characters programatically?
Note that this must occur in any text field across apps, so I can’t get the characters from the text field itself (unless there’s a way to access the currently in-focus text field).
What do you think?
There is no foolproof way to get the “current textbox”. Any application can roll out its own, so you don’t know what to look for. And if an application won’t cooperate on such things, you can’t really force it. Well, I guess you can always try to capture a screenshot and try to do OCR on it, but that’s not very realistic.
However, an application written using a widget toolkit of decent quality (or using stock controls) should support Microsoft Active Accessibility, which does provide a standardized way to access the UI of that application – or rather, some abstract view of that UI, which is optimized for extracting information from it by tools such as screen readers. Sounds like what you’d want, except that I do not know any way to retrieve information about text preceding the selection (a caret is just a 0-width selection) from a control using MSAA.