*/
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = super.keyPressed(component, keyCode, keyLocation);
TextInput textInput = (TextInput)getComponent();
Keyboard.Modifier commandModifier = Platform.getCommandModifier();
Keyboard.Modifier wordNavigationModifier = Platform.getWordNavigationModifier();
if (keyCode == Keyboard.KeyCode.DELETE && textInput.isEditable()) {
int index = textInput.getSelectionStart();
if (index < textInput.getCharacterCount()) {
int count = Math.max(textInput.getSelectionLength(), 1);
textInput.removeText(index, count);
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.BACKSPACE && textInput.isEditable()) {
int index = textInput.getSelectionStart();
int count = textInput.getSelectionLength();
if (count == 0
&& index > 0) {
textInput.removeText(index - 1, 1);
consumed = true;
} else {
textInput.removeText(index, count);
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.HOME
|| (keyCode == Keyboard.KeyCode.LEFT
&& Keyboard.isPressed(Keyboard.Modifier.META))) {
// 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);
}
scrollCharacterToVisible(0);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.END
|| (keyCode == Keyboard.KeyCode.RIGHT
&& Keyboard.isPressed(Keyboard.Modifier.META))) {
// Move the caret to the end of the text
int n = textInput.getCharacterCount();
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
int selectionStart = textInput.getSelectionStart();
textInput.setSelection(selectionStart, n - selectionStart);
} else {
textInput.setSelection(n, 0);
}
scrollCharacterToVisible(n);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.LEFT) {
int selectionStart = textInput.getSelectionStart();
int selectionLength = textInput.getSelectionLength();
if (Keyboard.isPressed(wordNavigationModifier)) {
// Move the caret to the start of the next word to the left
if (selectionStart > 0) {
// Skip over any space immediately to the left
int index = selectionStart;
while (index > 0
&& Character.isWhitespace(textInput.getCharacterAt(index - 1))) {
index--;
}
// Skip over any word-letters to the left
while (index > 0
&& !Character.isWhitespace(textInput.getCharacterAt(index - 1))) {
index--;
}
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
selectionLength += selectionStart - index;
} else {
selectionLength = 0;
}
selectionStart = index;
}
} else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the previous character to the selection
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
} else {
// Move the caret back by one character
if (selectionLength == 0
&& selectionStart > 0) {
selectionStart--;
}
// Clear the selection
selectionLength = 0;
}
if (selectionStart >= 0) {
textInput.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart);
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.RIGHT) {
int selectionStart = textInput.getSelectionStart();
int selectionLength = textInput.getSelectionLength();
if (Keyboard.isPressed(wordNavigationModifier)) {
// Move the caret to the start of the next word to the right
if (selectionStart < textInput.getCharacterCount()) {
int index = selectionStart + selectionLength;
// Skip over any space immediately to the right
while (index < textInput.getCharacterCount()
&& Character.isWhitespace(textInput.getCharacterAt(index))) {
index++;
}
// Skip over any word-letters to the right
while (index < textInput.getCharacterCount()
&& !Character.isWhitespace(textInput.getCharacterAt(index))) {
index++;
}
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
selectionLength = index - selectionStart;
} else {
selectionStart = index;
selectionLength = 0;
}
}
} else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the next character to the selection
selectionLength++;
} else {
// Move the caret forward by one character
if (selectionLength == 0) {
selectionStart++;
} else {
selectionStart += selectionLength;
}
// Clear the selection
selectionLength = 0;
}
if (selectionStart + selectionLength <= textInput.getCharacterCount()) {
textInput.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart + selectionLength);
consumed = true;
}
} else if (Keyboard.isPressed(commandModifier)) {
if (keyCode == Keyboard.KeyCode.A) {
textInput.setSelection(0, textInput.getCharacterCount());
consumed = true;
} else if (keyCode == Keyboard.KeyCode.X && textInput.isEditable()) {
if (textInput.isPassword()) {
Toolkit.getDefaultToolkit().beep();
} else {
textInput.cut();
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.C) {
if (textInput.isPassword()) {
Toolkit.getDefaultToolkit().beep();
} else {
textInput.copy();
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.V && textInput.isEditable()) {
textInput.paste();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.Z && textInput.isEditable()) {
if (!Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
textInput.undo();
}
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.INSERT) {
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && textInput.isEditable()) {
textInput.paste();
consumed = true;
}
} else {
consumed = super.keyPressed(component, keyCode, keyLocation);
}