0

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

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

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

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 文本编辑器。

有什么提示吗?

提前致谢

4

2 回答 2

1

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

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

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

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

textWidget.invokeAction(ST.TOGGLE_OVERWRITE);

另请注意,Mac 键盘没有插入键!

于 2016-06-22T11:49:10.817 回答
0

由于我发现在 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),我不知道对于较新的版本,这个“错误”是否已解决。

键绑定配置

于 2016-06-30T08:24:29.517 回答