@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 (textNode != null) {
Keyboard.Modifier commandModifier = Platform.getCommandModifier();
if (keyCode == Keyboard.KeyCode.DELETE
|| keyCode == Keyboard.KeyCode.BACKSPACE) {
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 {
Toolkit.getDefaultToolkit().beep();
}
} else {
textInput.delete(direction);
}
consumed = true;
} 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;
consumed = true;
} else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the previous character to the selection
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
consumed = true;
} else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Clear the selection and move the caret to the beginning of
// the text
selectionStart = 0;
selectionLength = 0;
consumed = true;
} else {
// Clear the selection and move the caret back by one
// character
if (selectionLength == 0
&& selectionStart > 0) {
selectionStart--;
consumed = true;
}
selectionLength = 0;
}
textInput.setSelection(selectionStart, selectionLength);
if (textNode.getCharacterCount() > 0) {
scrollCharacterToVisible(selectionStart);
} else {
setScrollLeft(0);
}
} 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;
consumed = true;
} else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the next character to the selection
if (selectionStart + selectionLength < textNode.getCharacterCount()) {
selectionLength++;
}
consumed = true;
} 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;
consumed = true;
} else {
// Clear the selection and move the caret forward by one
// character
selectionStart += selectionLength;
if (selectionLength == 0
&& selectionStart < textNode.getCharacterCount()) {
selectionStart++;
consumed = true;
}
selectionLength = 0;
}
textInput.setSelection(selectionStart, selectionLength);
if (textNode.getCharacterCount() > 0) {
scrollCharacterToVisible(selectionStart + selectionLength - 1);
} else {
scrollLeft = 0;
updateSelection();
}
} else if (keyCode == Keyboard.KeyCode.HOME) {
// Move the caret to the beginning of the text
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
textInput.setSelection(0, textInput.getSelectionStart());
} else {
textInput.setSelection(0, 0);
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.END) {
// Move the caret to the end of the text
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
int selectionStart = textInput.getSelectionStart();
textInput.setSelection(selectionStart, textNode.getCharacterCount()
- selectionStart);
} else {
textInput.setSelection(textNode.getCharacterCount(), 0);
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.A
&& Keyboard.isPressed(commandModifier)) {
// Select all
textInput.setSelection(0, textNode.getCharacterCount());
consumed = true;
} else if (keyCode == Keyboard.KeyCode.X
&& Keyboard.isPressed(commandModifier)) {
if (textInput.isPassword()) {
Toolkit.getDefaultToolkit().beep();
} else {
textInput.cut();
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.C
&& Keyboard.isPressed(commandModifier)) {
if (textInput.isPassword()) {
Toolkit.getDefaultToolkit().beep();
} else {
textInput.copy();
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.V
&& Keyboard.isPressed(commandModifier)) {
textInput.paste();
consumed = true;
} else {
consumed = super.keyPressed(component, keyCode, keyLocation);
}
}