簡體   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