public boolean keyTyped(char character, int key) { if (getTextField() != null) { if (key == 203) { cursorLeft(1); } else if (key == 205) { cursorRight(1); } else if (character == 22) { add(GuiScreen.getClipboardString()); } else if (key == 14) { delete(-1); } else if (key == 211) { delete(0); } else if (key == 28 || key == 156) { add("\n"); } else if (ChatAllowedCharacters.isAllowedCharacter(character)) { add(Character.toString(character)); } } return false; }
/** Call this method from you GuiScreen to process the keys into textbox. */ public boolean textboxKeyTyped(char par1, int par2) { if (this.isEnabled && this.isFocused) { switch (par1) { case 1: this.setCursorPositionEnd(); this.setSelectionPos(0); return true; case 3: GuiScreen.setClipboardString(this.getSelectedtext()); return true; case 22: this.writeText(GuiScreen.getClipboardString()); return true; case 24: GuiScreen.setClipboardString(this.getSelectedtext()); this.writeText(""); return true; default: switch (par2) { case 14: if (GuiScreen.isCtrlKeyDown()) { this.deleteWords(-1); } else { this.deleteFromCursor(-1); } return true; case 199: if (GuiScreen.isShiftKeyDown()) { this.setSelectionPos(0); } else { this.setCursorPositionZero(); } return true; case 203: if (GuiScreen.isShiftKeyDown()) { if (GuiScreen.isCtrlKeyDown()) { this.setSelectionPos(this.getNthWordFromPos(-1, this.getSelectionEnd())); } else { this.setSelectionPos(this.getSelectionEnd() - 1); } } else if (GuiScreen.isCtrlKeyDown()) { this.setCursorPosition(this.getNthWordFromCursor(-1)); } else { this.moveCursorBy(-1); } return true; case 205: if (GuiScreen.isShiftKeyDown()) { if (GuiScreen.isCtrlKeyDown()) { this.setSelectionPos(this.getNthWordFromPos(1, this.getSelectionEnd())); } else { this.setSelectionPos(this.getSelectionEnd() + 1); } } else if (GuiScreen.isCtrlKeyDown()) { this.setCursorPosition(this.getNthWordFromCursor(1)); } else { this.moveCursorBy(1); } return true; case 207: if (GuiScreen.isShiftKeyDown()) { this.setSelectionPos(this.text.length()); } else { this.setCursorPositionEnd(); } return true; case 211: if (GuiScreen.isCtrlKeyDown()) { this.deleteWords(1); } else { this.deleteFromCursor(1); } return true; default: if (ChatAllowedCharacters.isAllowedCharacter(par1)) { this.writeText(Character.toString(par1)); return true; } else { return false; } } } } else { return false; } }
/** Call this method from your GuiScreen to process the keys into the textbox */ public boolean textboxKeyTyped(char p_146201_1_, int p_146201_2_) { if (!this.field_146213_o) { return false; } else { switch (p_146201_1_) { case 1: this.func_146202_e(); this.func_146199_i(0); return true; case 3: GuiScreen.setClipboardString(this.func_146207_c()); return true; case 22: if (this.field_146226_p) { this.func_146191_b(GuiScreen.getClipboardString()); } return true; case 24: GuiScreen.setClipboardString(this.func_146207_c()); if (this.field_146226_p) { this.func_146191_b(""); } return true; default: switch (p_146201_2_) { case 14: if (GuiScreen.isCtrlKeyDown()) { if (this.field_146226_p) { this.func_146177_a(-1); } } else if (this.field_146226_p) { this.func_146175_b(-1); } return true; case 199: if (GuiScreen.isShiftKeyDown()) { this.func_146199_i(0); } else { this.func_146196_d(); } return true; case 203: if (GuiScreen.isShiftKeyDown()) { if (GuiScreen.isCtrlKeyDown()) { this.func_146199_i(this.func_146183_a(-1, this.func_146186_n())); } else { this.func_146199_i(this.func_146186_n() - 1); } } else if (GuiScreen.isCtrlKeyDown()) { this.func_146190_e(this.func_146187_c(-1)); } else { this.func_146182_d(-1); } return true; case 205: if (GuiScreen.isShiftKeyDown()) { if (GuiScreen.isCtrlKeyDown()) { this.func_146199_i(this.func_146183_a(1, this.func_146186_n())); } else { this.func_146199_i(this.func_146186_n() + 1); } } else if (GuiScreen.isCtrlKeyDown()) { this.func_146190_e(this.func_146187_c(1)); } else { this.func_146182_d(1); } return true; case 207: if (GuiScreen.isShiftKeyDown()) { this.func_146199_i(this.field_146216_j.length()); } else { this.func_146202_e(); } return true; case 211: if (GuiScreen.isCtrlKeyDown()) { if (this.field_146226_p) { this.func_146177_a(1); } } else if (this.field_146226_p) { this.func_146175_b(1); } return true; default: if (ChatAllowedCharacters.isAllowedCharacter(p_146201_1_)) { if (this.field_146226_p) { this.func_146191_b(Character.toString(p_146201_1_)); } return true; } else { return false; } } } } }
@Override public boolean onKeyTyped(char charTyped, int keyTyped) { if (!isFocused()) { return false; } switch (charTyped) { case 1: // ^A selectionEnd = caret = textLength; selectionStart = 0; findRenderStart(); return true; case 3: // ^C if (selectionStart != selectionEnd) { GuiScreen.setClipboardString(getSelectedText()); } return true; case 24: // ^X if (selectionStart != selectionEnd) { GuiScreen.setClipboardString(getSelectedText()); clearSelection(); } return true; case 22: // ^V writeText(GuiScreen.getClipboardString()); return true; default: switch (keyTyped) { case Keyboard.KEY_ESCAPE: setFocused(false); return !isFocused(); case Keyboard.KEY_RETURN: case Keyboard.KEY_NUMPADENTER: return onEnter(); case Keyboard.KEY_INSERT: if (GuiScreen.isShiftKeyDown()) { writeText(GuiScreen.getClipboardString()); } else { caretInsert = !caretInsert; } return true; case Keyboard.KEY_CLEAR: // mac only (clear selection) clearSelection(); return true; case Keyboard.KEY_DELETE: // delete if (!GuiScreen.isShiftKeyDown()) { if (selectionStart != selectionEnd) { clearSelection(); } else if (GuiScreen.isCtrlKeyDown()) { int size = seekNextCaretLocation(caret, true) - caret; selectionStart = caret; selectionEnd = caret + size; clearSelection(); } else { if (caret < textLength && textLength > 0) { --textLength; System.arraycopy(text, caret + 1, text, caret, textLength - caret); } } if (caret <= renderStart) { renderStart = MathHelper.clampI(caret - 3, 0, textLength); } findRenderStart(); onCharacterEntered(true); return true; } // continue.. (shift+delete = backspace) case Keyboard.KEY_BACK: // backspace if (selectionStart != selectionEnd) { clearSelection(); } else if (GuiScreen.isCtrlKeyDown()) { int size = seekNextCaretLocation(caret, false) - caret; selectionStart = caret + size; selectionEnd = caret; clearSelection(); } else { if (caret > 0 && textLength > 0) { --caret; System.arraycopy(text, caret + 1, text, caret, textLength - caret); --textLength; } } if (caret <= renderStart) { renderStart = MathHelper.clampI(caret - 3, 0, textLength); } findRenderStart(); onCharacterEntered(true); return true; case Keyboard.KEY_HOME: // home if (GuiScreen.isShiftKeyDown()) { if (caret > selectionEnd) { selectionEnd = selectionStart; } selectionStart = 0; } else { selectionStart = selectionEnd = 0; } renderStart = caret = 0; return true; case Keyboard.KEY_END: // end if (GuiScreen.isShiftKeyDown()) { if (caret < selectionStart) { selectionStart = selectionEnd; } selectionEnd = textLength; } else { selectionStart = selectionEnd = textLength; } caret = textLength; findRenderStart(); return true; case Keyboard.KEY_LEFT: // left arrow case Keyboard.KEY_RIGHT: // right arrow int size = keyTyped == 203 ? -1 : 1; if (GuiScreen.isCtrlKeyDown()) { size = seekNextCaretLocation(caret, keyTyped == 205) - caret; } if (selectionStart == selectionEnd || !GuiScreen.isShiftKeyDown()) { selectionStart = selectionEnd = caret; } { int t = caret; caret = MathHelper.clampI(caret + size, 0, textLength); size = caret - t; } findRenderStart(); if (GuiScreen.isShiftKeyDown()) { if (caret == selectionStart + size) { selectionStart = caret; } else if (caret == selectionEnd + size) { selectionEnd = caret; } // this logic is 'broken' in that the selection doesn't wrap // such that a|bc|def becomes abc|def| but it will highlight // the rest of the word the caret is on i.e., a|bc|def -> a|bcdef| // i don't know that it matters (home+end exhibit the former) if (selectionStart > selectionEnd) { int t = selectionStart; selectionStart = selectionEnd; selectionEnd = t; } } return true; default: if (isAllowedCharacter(charTyped)) { boolean typed = insertCharacter(charTyped); clearSelection(); findRenderStart(); onCharacterEntered(typed); return true; } else { return false; } } } }