Eclipse E4 RCP StyledText 获取 INSERT KEY 状态

Eclipse E4 RCP StyledText obtain INSERT KEY state

对于使用 StyledText/SourceViewer 实现的 RCP E4 文本编辑器应用程序,有必要接收插入键的状态。

一旦收到状态(插入、智能插入),应用程序应修改光标图标并通知其他部分插入状态(即像普通纯文本编辑器行为一样通知状态栏控件)。

SWT.INSERT 仅监听要按下的键,但如果 StyledText 处于插入模式则不监听。

styledText.addKeyListener(new KeyAdapter(){
    public void keyPressed(KeyEvent e){
        if(e.keyCode == SWT.INSERT){
            System.out.println("INSERT KEY PRESSED!!!");
        }
    }
};

我已经避免延长

org.eclipse.ui.texteditor.AbstractTextEditor

并使用方法

getInsertMode()

因为该应用程序旨在成为纯 E4 文本编辑器。

有什么提示吗?

提前致谢

首先,您需要告诉 StyledText 当它看到 Insert 键时不要执行默认操作:

textWidget.setKeyBinding(SWT.INSERT, SWT.NULL);

接下来需要在上下文中定义一个Command、Handler和Key Binding,供编辑器处理Insert键。

插入命令的处理程序可以更新状态显示,然后shoyld告诉StyledText更新覆盖模式:

textWidget.invokeAction(ST.TOGGLE_OVERWRITE);

另请注意 Mac 键盘没有 Insert 键!

由于我发现在 E4 RCP 文本编辑器的 sourceviewer 控件中处理 INSERT_KEY 有一些困难,我将在 gregg449 的回答中写下额外的细节(他每次都提供了很大的帮助!)。

根据上述答案,我创建了绑定上下文、绑定 Table、命令、处理程序,并将绑定上下文添加到所需的部分(实现 SourceViewer 的部分)。

下一个代码用于 SourceViewer 和 InserKey 处理程序:

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...
        }
    }
}

下图显示了创建了绑定上下文和绑定 Table 的 Application.e4xmi。 请注意,如果您不将补充标记 "type:user" 添加到绑定 Table,则绑定根本不起作用。 这没有反映在 vogella 的教程 (http://www.vogella.com/tutorials/EclipseRCP/article.html) 和他的书中。

我唯一找到此信息的地方是在 Whosebug 问题上:

我正在为 Linux 和 Windows 使用 eclipse Mars (4.5.0),我不知道对于较新的版本这个 'bug' 是否已解决。