Reputation: 77
I'm developing a voice recognition plugin for Eclipse. For example when I am writing code I can simply say "for" and "for(){}" will be printed to the editor. It works great, however, when the scope of the cursor goes out of Eclipse e.g. when I click on a browser search the next thing I say will be printed here instead of the Eclipse editor.
Is there a way to find out where the caret position is on the Eclipse editor when it goes out of scope, because when you click on any part of the Eclipse UI the old caret position pops back up.
I am also using the JavaRobot API to emulate the string to the screen. I don't believe it has the option of setting the caret position, only the courser position.
Update: All that is needed is the text editor caret position x and y co-ordinates, I can use the Java Robot move mouse(caret position x,y) and mouse press to bring the eclipse platform back into scope before any emulation occurs.
Upvotes: 1
Views: 1158
Reputation: 111217
Assuming you have the IEditorPart
you are interested in you can get the caret position using:
IEditorPart editor = .... get part ..
Control control = editor.getAdapter(Control.class);
// For a text editor the control will be StyledText
if (control instanceof StyledText) {
StyledText text = (StyledText)control;
// Position of caret relative to top left of the control
Point relPos = text.getLocationAtOffset(text.getCaretOffset());
// Position relative to display
Point absPos = textWidget.toDisplay(relPos);
}
Upvotes: 1