由于我发现在 E4 RCP 文本编辑器的 sourceviewer 控件中处理 INSERT_KEY 存在一些困难,因此我将在 gregg449 的答案中写下额外的细节(每次都得到他的大力帮助!)。
按照上面的答案,我创建了绑定上下文、绑定表、命令、处理程序,并将绑定上下文添加到所需的部分(实现 SourceViewer 的部分)。
下一个代码用于 SourceViewer 和 InserKey Handler:
public class CheckKeyBindingSourceViewer extends ITextEditorPart{
public SourceViewer sv = null;
public StyledText st = null;
@PostConstruct
public void postConstruct(Composite parent) {
sv = new SourceViewer(parent, null, null, true, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
IDocument doc = new Document("");
sv.setDocument(doc);
st = sv.getTextWidget();
//tell the StyledText not to do the default action when it sees the Insert key
st.setKeyBinding(SWT.INSERT, SWT.NULL);
}
}
public class InsertKeyHandler {
@Execute
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
if (activePart.getObject() instanceof ITextEditorPart){
ITextEditorPart theSourceViewer = (ITextEditorPart) activePart.getObject();
theSourceViewer.st.invokeAction(ST.TOGGLE_OVERWRITE);
//TODO
//Change cursor sourcewiewer, notify to Statusbar...
}
}
}
下图显示了创建了绑定上下文和绑定表的 Application.e4xmi。请注意,如果您不将补充标记“type:user”添加到绑定表,则绑定根本不起作用。这没有反映在 vogella 的教程 ( http://www.vogella.com/tutorials/EclipseRCP/article.html ) 和他的书中。
我发现此信息的唯一地方是 stackoverflow 问题:
eclipse rcp keybindings don't work
我在 Linux 和 Windows 上都使用 eclipse Mars (4.5.0),我不知道对于较新的版本,这个“错误”是否已解决。
