I am developing an (rich) editor based on SWT StyledText
. There is one feature that I until now cannot solve it. I want my editor to place the cursor at the tab width as the beginning of the previous line when the user presses Ctrl+u (similar to Eclipse or Notepad++ when users press Enter key). I've tried several methods but nothing works for me. Please take a look at my example. Every suggestion is welcome. Thanks in advance.
StyledText text = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
text.setTabs(5);
text.setText("");
text.setLeftMargin(5);
text.setBounds(0, 0, 512, 391);
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int currentLine = text.getLineAtOffset(text.getCaretOffset());
int currCaretOffset = text.getCaretOffset();
if(e.stateMask == SWT.CTRL && e.keyCode == 'u'){
//text.setIndent(text.getOffsetAtLine(currentLine));//doesn't work
text.append("\n");
//text.append("\t");//doesn't work
text.setCaretOffset(text.getCharCount()+text.getTabs());//doesn't work
System.out.println("caret offset "+text.getCaretOffset());
}
}
});