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) return false; boolean repeat = false; boolean ctrl = UIUtils.ctrl(); boolean jump = ctrl && !passwordMode; if (ctrl) { if (keycode == Keys.V) { paste(clipboard.getContents(), true); repeat = true; } if (keycode == Keys.C || keycode == Keys.INSERT) { copy(); return true; } if (keycode == Keys.X) { cut(true); return true; } if (keycode == Keys.A) { selectAll(); return true; } if (keycode == Keys.Z) { String oldText = text; setText(undoText); undoText = oldText; updateDisplayText(); return true; } } if (UIUtils.shift()) { if (keycode == Keys.INSERT) paste(clipboard.getContents(), true); if (keycode == Keys.FORWARD_DEL) cut(true); selection: { int temp = cursor; keys: { if (keycode == Keys.LEFT) { moveCursor(false, jump); repeat = true; break keys; } if (keycode == Keys.RIGHT) { moveCursor(true, jump); repeat = true; break keys; } if (keycode == Keys.HOME) { goHome(jump); break keys; } if (keycode == Keys.END) { goEnd(jump); break keys; } break selection; } if (!hasSelection) { selectionStart = temp; hasSelection = true; } } } else { // Cursor movement or other keys (kills selection). if (keycode == Keys.LEFT) { moveCursor(false, jump); clearSelection(); repeat = true; } if (keycode == Keys.RIGHT) { moveCursor(true, jump); clearSelection(); repeat = true; } if (keycode == Keys.HOME) { goHome(jump); clearSelection(); } if (keycode == Keys.END) { goEnd(jump); clearSelection(); } } cursor = MathUtils.clamp(cursor, 0, text.length()); if (repeat) { scheduleKeyRepeatTask(keycode); } return true; }
public boolean keyTyped(InputEvent event, char character) { if (disabled) return false; // Disallow "typing" most ASCII control characters, which would show up as a space when // onlyFontChars is true. switch (character) { case BACKSPACE: case TAB: case ENTER_ANDROID: case ENTER_DESKTOP: break; default: if (character < 32) return false; } Stage stage = getStage(); if (stage == null || stage.getKeyboardFocus() != TextField.this) return false; if (UIUtils.isMac && Gdx.input.isKeyPressed(Keys.SYM)) return true; if ((character == TAB || character == ENTER_ANDROID) && focusTraversal) { next(UIUtils.shift()); } else { boolean delete = character == DELETE; boolean backspace = character == BACKSPACE; boolean enter = character == ENTER_DESKTOP || character == ENTER_ANDROID; boolean add = enter ? writeEnters : (!onlyFontChars || style.font.getData().hasGlyph(character)); boolean remove = backspace || delete; if (add || remove) { String oldText = text; int oldCursor = cursor; if (hasSelection) cursor = delete(false); else { if (backspace && cursor > 0) { text = text.substring(0, cursor - 1) + text.substring(cursor--); renderOffset = 0; } if (delete && cursor < text.length()) { text = text.substring(0, cursor) + text.substring(cursor + 1); } } if (add && !remove) { // Character may be added to the text. if (!enter && filter != null && !filter.acceptChar(TextField.this, character)) return true; if (!withinMaxLength(text.length())) return true; String insertion = enter ? "\n" : String.valueOf(character); text = insert(cursor++, insertion, text); } String tempUndoText = undoText; if (changeText(oldText, text)) { long time = System.currentTimeMillis(); if (time - 750 > lastChangeTime) undoText = oldText; lastChangeTime = time; } else cursor = oldCursor; updateDisplayText(); } } if (listener != null) listener.keyTyped(TextField.this, character); return true; }