繁体   English   中英

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

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

我正在为Eclipse开发语音识别插件。 例如,当我编写代码时,我可以简单地说“ for”和“ for(){}”将被打印到编辑器中。 但是,当光标的范围超出Eclipse时(例如,当我单击浏览器搜索时),它会很好地工作,我说的下一个内容将在此处打印而不是Eclipse编辑器。

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

我还使用JavaRobot API将字符串模拟到屏幕上。 我不相信它可以设置插入符号位置,而只能设置粗野位置。

更新:所需的只是文本编辑器的插入位置x和y坐标,我可以使用Java Robot移动鼠标(插入位置x,y)和鼠标按下来使Eclipse平台重新进入范围,然后再进行任何仿真。

假设您对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