@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = super.keyPressed(component, keyCode, keyLocation);
TextInput textInput = (TextInput)getComponent();
TextNode textNode = textInput.getTextNode();
if (keyCode == Keyboard.KeyCode.DELETE
|| keyCode == Keyboard.KeyCode.BACKSPACE) {
consumed = true;
Direction direction = (keyCode == Keyboard.KeyCode.DELETE ?
Direction.FORWARD : Direction.BACKWARD);
Validator validator = textInput.getValidator();
if (validator != null
&& strictValidation) {
StringBuilder buf = new StringBuilder(textNode.getText());
int index = textInput.getSelectionStart();
int count = textInput.getSelectionLength();
if (count > 0) {
buf.delete(index, index + count);
} else {
if (direction == Direction.BACKWARD) {
index--;
}
if (index >= 0
&& index < textNode.getCharacterCount()) {
buf.deleteCharAt(index);
}
}
if (validator.isValid(buf.toString())) {
textInput.delete(direction);
} else {
ApplicationContext.beep();
}
} else {
textInput.delete(direction);
}
} else if (keyCode == Keyboard.KeyCode.LEFT) {
consumed = true;
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) {
if (selectionStart > 0) {
selectionStart--;
} else {
consumed = false;
}
}
selectionLength = 0;
}
textInput.setSelection(selectionStart, selectionLength);
} else if (keyCode == Keyboard.KeyCode.RIGHT) {
consumed = true;
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) {
if (selectionStart < textNode.getCharacterCount()) {
selectionStart++;
} else {
consumed = false;
}
}
selectionLength = 0;
}
textInput.setSelection(selectionStart, selectionLength);
} else if (keyCode == Keyboard.KeyCode.HOME) {
consumed = true;
// Move the caret to the beginning of the text
textInput.setSelection(0, 0);
} else if (keyCode == Keyboard.KeyCode.END) {
consumed = true;
// Move the caret to the end of the text
textInput.setSelection(textNode.getCharacterCount(), 0);
} else if (keyCode == Keyboard.KeyCode.A
&& Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
consumed = true;
// Select all
textInput.setSelection(0, textNode.getCharacterCount());
} else if (keyCode == Keyboard.KeyCode.X
&& Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
consumed = true;
if (textInput.isPassword()) {