eclipsee4styledtext

Eclipse E4 RCP StyledText obtain INSERT KEY state


For an RCP E4 Text Editor application implemented with a StyledText/SourceViewer it is necessary receive the status of the inset key.

Once received the state (insert, smart-insert), the application shall modify the cursor icon and notify other parts the INSERT state (i.e. notify to the status bar control like in a normal plain text editor behavior).

SWT.INSERT only listens for the key to be pressed, but nothing if the StyledText is in INSERT MODE.

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

I have avoided to extend

org.eclipse.ui.texteditor.AbstractTextEditor

and use the method

getInsertMode()

since the application is intended to be pure E4 text editor.

Any hint?

Thanks in advance


Solution

  • First off you need to tell the StyledText not to do the default action when it sees the Insert key:

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

    Next you need to define a Command, Handler and Key Binding in the context for the editor to deal with the Insert key.

    The Handler for the insert command can update the status display and shoyld then tell the StyledText to update the overwrite mode:

    textWidget.invokeAction(ST.TOGGLE_OVERWRITE);
    

    Also note that Mac keyboards don't have an Insert key!