【问题标题】:Eclipse:Get and set caret position of the editorEclipse:获取和设置编辑器的插入符号位置
【发布时间】:2016-02-24 01:06:36
【问题描述】:

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

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

我还使用 JavaRobot API 将字符串模拟到屏幕上。我不相信它有设置插入符号位置的选项,只有更粗略的位置。

更新:只需要文本编辑器插入符号位置 x 和 y 坐标,我可以使用 Java 机器人移动鼠标(插入符号位置 x,y)和鼠标按下将 eclipse 平台带回范围之前发生仿真。

【问题讨论】:

  • 你可能想看看here

标签: java eclipse eclipse-plugin


【解决方案1】:

假设您有您感兴趣的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); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多