default void scroll(int units, double u, double v) {
}
+ /** True for panels that consume typed CHARACTERS (terminals, editors).
+ * GUI applications take key PRESSES only: X has no typed event and
+ * forwarding both would deliver every character twice. */
+ default boolean acceptsTypedText() {
+ return false;
+ }
+
/** Key event while the panel holds keyboard focus. */
default void key(int code, char character, boolean pressed) {
}
private static final int VK_BACK_SPACE = 8, VK_TAB = 9, VK_ENTER = 10;
private static final int VK_LEFT = 37, VK_UP = 38, VK_RIGHT = 39, VK_DOWN = 40;
+ /** A terminal wants typed CHARACTERS (KEY_TYPED), never key presses. */
+ @Override
+ public boolean acceptsTypedText() {
+ return true;
+ }
+
/**
- * Keyboard into the PTY. A line discipline wants CR for Enter, never LF,
- * and backspace is DEL, not BS - both are terminal conventions, not
- * engine ones, so they live with the panel.
+ * Keyboard into the PTY.
+ *
+ * <p>CRITICAL: the shell fills BOTH KEY_TYPED and KEY_PRESSED events with
+ * the produced character (AWT gives getKeyChar() on presses too). Text is
+ * therefore taken ONLY from KEY_TYPED events (code 0) and control keys
+ * ONLY from presses - otherwise every typed letter arrives twice.
+ *
+ * <p>A line discipline wants CR for Enter, never LF, and backspace is
+ * DEL, not BS: terminal conventions, so they live with the panel.
*/
@Override
public void key(final int code, final char character, final boolean pressed) {
if (!pressed) return;
- if (character == '\n' || character == '\r') {
- send("\r");
- } else if (character >= 32 || character == '\t' || character == '\b'
- || character == 127) {
- send(String.valueOf(character));
- } else {
- switch (code) {
- case VK_ENTER -> send("\r");
- case VK_BACK_SPACE -> send("\u007f");
- case VK_TAB -> send("\t");
- case VK_UP -> send("\u001b[A");
- case VK_DOWN -> send("\u001b[B");
- case VK_RIGHT -> send("\u001b[C");
- case VK_LEFT -> send("\u001b[D");
- default -> {
- }
+ if (code == 0) {
+ // typed text; control characters were already handled as presses
+ if (character >= 32 || character == '\t') {
+ send(String.valueOf(character));
+ }
+ return;
+ }
+ switch (code) {
+ case VK_ENTER -> send("\r");
+ case VK_BACK_SPACE -> send("\u007f");
+ case VK_TAB -> send("\t");
+ case VK_UP -> send("\u001b[A");
+ case VK_DOWN -> send("\u001b[B");
+ case VK_RIGHT -> send("\u001b[C");
+ case VK_LEFT -> send("\u001b[D");
+ default -> {
}
}
}
}
if (focused == null) return;
if (event.type() == InputStore.Event.Type.KEY_TYPED) {
- // a typed character is TEXT, not a press: it carries no key code
- // and must reach the panel even though the key is still held
- focused.key(0, event.character(), true);
+ // TEXT is for panels that want characters. An X11 application
+ // gets the text from its own press event, so forwarding this too
+ // would type every character twice.
+ if (focused.acceptsTypedText()) {
+ focused.key(0, event.character(), true);
+ }
return;
}
if (pressed) {