if (pointer == 0 && button != 0) return false;
if (disabled) return true;
clearSelection();
setCursorPosition(x);
selectionStart = cursor;
Stage stage = getStage();
if (stage != null) stage.setKeyboardFocus(TextField.this);
keyboard.show(true);
return true;
}
public void touchDragged (InputEvent event, float x, float y, int pointer) {
super.touchDragged(event, x, y, pointer);
lastBlink = 0;
cursorOn = false;
setCursorPosition(x);
hasSelection = true;
}
private void setCursorPosition (float x) {
lastBlink = 0;
cursorOn = false;
x -= renderOffset + textOffset;
for (int i = 0; i < glyphPositions.size; i++) {
if (glyphPositions.items[i] > x) {
cursor = Math.max(0, i - 1);
return;
}
}
cursor = Math.max(0, glyphPositions.size - 1);
}
public boolean keyDown (InputEvent event, int keycode) {
if (disabled) return false;
lastBlink = 0;
cursorOn = false;
Stage stage = getStage();
if (stage != null && stage.getKeyboardFocus() == TextField.this) {
boolean repeat = false;
boolean ctrl;
if (isMac)
ctrl = Gdx.input.isKeyPressed(Keys.SYM);
else
ctrl = Gdx.input.isKeyPressed(Keys.CONTROL_LEFT) || Gdx.input.isKeyPressed(Keys.CONTROL_RIGHT);
if (ctrl) {
// paste
if (keycode == Keys.V) {
paste();
return true;
}
// copy
if (keycode == Keys.C || keycode == Keys.INSERT) {
copy();
return true;
}
// cut
if (keycode == Keys.X || keycode == Keys.DEL) {
cut();
return true;
}
// select all
if (keycode == Keys.A) {
selectAll();
return true;
}
}
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) || Gdx.input.isKeyPressed(Keys.SHIFT_RIGHT)) {
// paste
if (keycode == Keys.INSERT) paste();
// cut
if (keycode == Keys.FORWARD_DEL) {
if (hasSelection) {
copy();
delete();
}
}
// selection
if (keycode == Keys.LEFT) {
if (!hasSelection) {
selectionStart = cursor;
hasSelection = true;
}
while (--cursor > 0 && ctrl) {
char c = text.charAt(cursor);
if (c >= 'A' && c <= 'Z') continue;
if (c >= 'a' && c <= 'z') continue;
if (c >= '0' && c <= '9') continue;
break;
}
repeat = true;
}
if (keycode == Keys.RIGHT) {
if (!hasSelection) {
selectionStart = cursor;
hasSelection = true;
}
int length = text.length();
while (++cursor < length && ctrl) {
char c = text.charAt(cursor - 1);
if (c >= 'A' && c <= 'Z') continue;
if (c >= 'a' && c <= 'z') continue;
if (c >= '0' && c <= '9') continue;
break;
}
repeat = true;
}
if (keycode == Keys.HOME) {
if (!hasSelection) {
selectionStart = cursor;
hasSelection = true;
}
cursor = 0;
}
if (keycode == Keys.END) {
if (!hasSelection) {
selectionStart = cursor;
hasSelection = true;
}
cursor = text.length();
}
cursor = Math.max(0, cursor);
cursor = Math.min(text.length(), cursor);
} else {
// cursor movement or other keys (kill selection)
if (keycode == Keys.LEFT) {
while (cursor-- > 1 && ctrl) {
char c = text.charAt(cursor - 1);
if (c >= 'A' && c <= 'Z') continue;
if (c >= 'a' && c <= 'z') continue;
if (c >= '0' && c <= '9') continue;
break;
}
clearSelection();
repeat = true;
}
if (keycode == Keys.RIGHT) {
int length = text.length();
while (++cursor < length && ctrl) {
char c = text.charAt(cursor - 1);
if (c >= 'A' && c <= 'Z') continue;
if (c >= 'a' && c <= 'z') continue;
if (c >= '0' && c <= '9') continue;
break;
}
clearSelection();
repeat = true;
}
if (keycode == Keys.HOME) {
cursor = 0;
clearSelection();
}
if (keycode == Keys.END) {
cursor = text.length();
clearSelection();
}
cursor = Math.max(0, cursor);
cursor = Math.min(text.length(), cursor);
}
if (repeat && (!keyRepeatTask.isScheduled() || keyRepeatTask.keycode != keycode)) {
keyRepeatTask.keycode = keycode;
keyRepeatTask.cancel();
Timer.schedule(keyRepeatTask, keyRepeatInitialTime, keyRepeatTime);
}
return true;
}
return false;
}
public boolean keyUp (InputEvent event, int keycode) {
if (disabled) return false;
keyRepeatTask.cancel();
return true;
}
public boolean keyTyped (InputEvent event, char character) {
if (disabled) return false;
final BitmapFont font = style.font;
Stage stage = getStage();
if (stage != null && stage.getKeyboardFocus() == TextField.this) {
if (character == BACKSPACE) {
if (cursor > 0 || hasSelection) {
if (!hasSelection) {
text = text.substring(0, cursor - 1) + text.substring(cursor);
updateDisplayText();