@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = false;
TextInput textInput = (TextInput)getComponent();
TextNode textNode = textInput.getTextNode();
if (keyCode == Keyboard.KeyCode.DELETE) {
textInput.delete(Direction.FORWARD);
} else if (keyCode == Keyboard.KeyCode.BACKSPACE) {
textInput.delete(Direction.BACKWARD);
} else if (keyCode == Keyboard.KeyCode.LEFT) {
int selectionStart = textInput.getSelectionStart();
int selectionLength = textInput.getSelectionLength();
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)
&& Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Add all preceding text to the selection
selectionLength = selectionStart + selectionLength;
selectionStart = 0;
} else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the previous character to the selection
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
} else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Clear the selection and move the caret to the beginning of
// the text
selectionStart = 0;
selectionLength = 0;
} else {
// Clear the selection and move the caret back by one
// character
if (selectionLength == 0
&& selectionStart > 0) {
selectionStart--;
}
selectionLength = 0;
}
textInput.setSelection(selectionStart, selectionLength);
} else if (keyCode == Keyboard.KeyCode.RIGHT) {
int selectionStart = textInput.getSelectionStart();
int selectionLength = textInput.getSelectionLength();
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)
&& Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Add all subsequent text to the selection
selectionLength = textNode.getCharacterCount() - selectionStart;
} else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the next character to the selection
if (selectionStart + selectionLength < textNode.getCharacterCount()) {
selectionLength++;
}
} else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Clear the selection and move the caret to the end of
// the text
selectionStart = textNode.getCharacterCount();
selectionLength = 0;
} else {
// Clear the selection and move the caret forward by one
// character
selectionStart += selectionLength;
if (selectionLength == 0
&& selectionStart < textNode.getCharacterCount()) {
selectionStart++;
}
selectionLength = 0;
}
textInput.setSelection(selectionStart, selectionLength);
} else if (keyCode == Keyboard.KeyCode.A
&& Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Select all
textInput.setSelection(0, textNode.getCharacterCount());
} else if (keyCode == Keyboard.KeyCode.X
&& Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
if (textInput.isPassword()) {
ApplicationContext.beep();
} else {