简体   繁体   English

Eclipse:获取并设置编辑器的插入位置

[英]Eclipse:Get and set caret position of the editor

I'm developing a voice recognition plugin for Eclipse. 我正在为Eclipse开发语音识别插件。 For example when I am writing code I can simply say "for" and "for(){}" will be printed to the editor. 例如,当我编写代码时,我可以简单地说“ for”和“ for(){}”将被打印到编辑器中。 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. 但是,当光标的范围超出Eclipse时(例如,当我单击浏览器搜索时),它会很好地工作,我说的下一个内容将在此处打印而不是Eclipse编辑器。

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. 当它超出范围时,是否有办法找出插入符号位置在Eclipse编辑器上的原因,因为当您单击Eclipse UI的任何部分时,旧的插入符号位置会重新弹出。

I am also using the JavaRobot API to emulate the string to the screen. 我还使用JavaRobot API将字符串模拟到屏幕上。 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. 更新:所需的只是文本编辑器的插入位置x和y坐标,我可以使用Java Robot移动鼠标(插入位置x,y)和鼠标按下来使Eclipse平台重新进入范围,然后再进行任何仿真。

Assuming you have the IEditorPart you are interested in you can get the caret position using: 假设您对IEditorPart感兴趣,则可以使用以下命令获得插入符号位置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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