简体   繁体   中英

Eclipse:Get and set caret position of the editor

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 eg 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.

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); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM