Java Code Examples for com.googlecode.lanterna.input.KeyType#Escape

The following examples show how to use com.googlecode.lanterna.input.KeyType#Escape . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Issue312.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    Terminal terminal = new DefaultTerminalFactory().createTerminal();
    TextGraphics textGraphics = terminal.newTextGraphics();
    int row = 0;
    while(true) {
        KeyStroke keyStroke = terminal.pollInput();
        if (keyStroke == null) {
            terminal.setCursorPosition(0, 0);
            textGraphics.putString(0, terminal.getCursorPosition().getRow(), " > ");
            terminal.flush();
            keyStroke = terminal.readInput();
            row = 1;
            terminal.clearScreen();
        }
        if (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF) {
            break;
        }
        textGraphics.putString(0, row++, "Read KeyStroke: " + keyStroke + "\n");
        terminal.flush();
    }
    terminal.close();
}
 
Example 2
Source File: AbstractWindow.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean handleInput(KeyStroke key) {
    boolean handled = super.handleInput(key);
    if(!handled && closeWindowWithEscape && key.getKeyType() == KeyType.Escape) {
        close();
        return true;
    }
    return handled;
}
 
Example 3
Source File: ComboBox.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public synchronized boolean handleInput(KeyStroke keyStroke) {
    if (keyStroke.getKeyType() == KeyType.Escape) {
        close();
        popupWindow = null;
        return true;
    }
    return super.handleInput(keyStroke);
}
 
Example 4
Source File: SwingTerminalFrame.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke pollInput() {
    if(disposed) {
        return new KeyStroke(KeyType.EOF);
    }
    KeyStroke keyStroke = swingTerminal.pollInput();
    if(autoCloseTriggers.contains(TerminalEmulatorAutoCloseTrigger.CloseOnEscape) &&
            keyStroke != null && 
            keyStroke.getKeyType() == KeyType.Escape) {
        dispose();
    }
    return keyStroke;
}
 
Example 5
Source File: AWTTerminalFrame.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke pollInput() {
    if(disposed) {
        return new KeyStroke(KeyType.EOF);
    }
    KeyStroke keyStroke = awtTerminal.pollInput();
    if(autoCloseTriggers.contains(TerminalEmulatorAutoCloseTrigger.CloseOnEscape) &&
            keyStroke != null && 
            keyStroke.getKeyType() == KeyType.Escape) {
        dispose();
    }
    return keyStroke;
}
 
Example 6
Source File: VirtualScreenTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public VirtualScreenTest(String[] args) throws InterruptedException, IOException {
    Screen screen = new TestTerminalFactory(args).createScreen();
    screen = new VirtualScreen(screen);
    screen.startScreen();

    TextGraphics textGraphics = screen.newTextGraphics();
    textGraphics.setBackgroundColor(TextColor.ANSI.GREEN);
    textGraphics.fillTriangle(new TerminalPosition(40, 0), new TerminalPosition(25,19), new TerminalPosition(65, 19), ' ');
    textGraphics.setBackgroundColor(TextColor.ANSI.RED);
    textGraphics.drawRectangle(TerminalPosition.TOP_LEFT_CORNER, screen.getTerminalSize(), ' ');
    screen.refresh();

    while(true) {
        KeyStroke keyStroke = screen.pollInput();
        if(keyStroke != null) {
            if(keyStroke.getKeyType() == KeyType.Escape) {
                break;
            }
        }
        else if(screen.doResizeIfNecessary() != null) {
            screen.refresh();
        }
        else {
            Thread.sleep(1);
        }
    }
    screen.stopScreen();
}
 
Example 7
Source File: TerminalInputTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, IOException {
    // For IDE users: either set runtime arguments or uncomment this line:
    //args = new String[] { "--mouse-move", "--telnet-port=1024", "--with-timeout=12" };

    final Terminal rawTerminal = new TestTerminalFactory(args).createTerminal();
    rawTerminal.enterPrivateMode();

    int currentRow = 0;
    rawTerminal.setCursorPosition(0, 0);
    while(true) {
        KeyStroke key = rawTerminal.pollInput();
        if(key == null) {
            Thread.sleep(1);
            continue;
        }

        if(key.getKeyType() == KeyType.Escape || key.getKeyType() == KeyType.EOF) {
            break;
        }

        if(currentRow == 0) {
            rawTerminal.clearScreen();
        }

        rawTerminal.setCursorPosition(0, currentRow++);
        putString(rawTerminal, key.toString());

        if(currentRow >= rawTerminal.getTerminalSize().getRows()) {
            currentRow = 0;
        }
    }

    rawTerminal.exitPrivateMode();
}
 
Example 8
Source File: PrivateModeTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    Terminal terminal = new TestTerminalFactory(args)
                            .setTerminalEmulatorFrameAutoCloseTrigger(null)
                            .createTerminal();
    boolean normalTerminal = true;
    printNormalTerminalText(terminal);
    KeyStroke keyStroke = null;
    while(keyStroke == null || keyStroke.getKeyType() != KeyType.Escape) {
        keyStroke = terminal.pollInput();
        if(keyStroke != null && keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ') {
            normalTerminal = !normalTerminal;
            if(normalTerminal) {
                terminal.exitPrivateMode();
                printNormalTerminalText(terminal);
            }
            else {
                terminal.enterPrivateMode();
                printPrivateModeTerminalText(terminal);
            }
        }
        else {
            Thread.sleep(1);
        }
    }
    if(!normalTerminal) {
        terminal.exitPrivateMode();
    }
    terminal.putCharacter('\n');
    if(terminal instanceof Window) {
        ((Window) terminal).dispose();
    }
}
 
Example 9
Source File: ScreenTriangleTest.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    boolean useAnsiColors = false;
    boolean useFilled = false;
    boolean slow = false;
    boolean rotating = false;
    boolean square = false;
    for(String arg : args) {
        if(arg.equals("--ansi-colors")) {
            useAnsiColors = true;
        }
        if(arg.equals("--filled")) {
            useFilled = true;
        }
        if(arg.equals("--slow")) {
            slow = true;
        }
        if(arg.equals("--rotating")) {
            rotating = true;
        }
        if(arg.equals("--square")) {
            square = true;
        }
    }
    Screen screen = new TestTerminalFactory(args).createScreen();
    screen.startScreen();

    TextGraphics graphics = new ScreenTextGraphics(screen);
    if(square) {
        graphics = new DoublePrintingTextGraphics(graphics);
    }
    Random random = new Random();

    TextColor color = null;
    double rad = 0.0;
    while(true) {
        KeyStroke keyStroke = screen.pollInput();
        if(keyStroke != null &&
                (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) {
            break;
        }
        screen.doResizeIfNecessary();
        TerminalSize size = graphics.getSize();
        if(useAnsiColors) {
            if(color == null || !rotating) {
                color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)];
            }
        }
        else {
            if(color == null || !rotating) {
                //Draw a rectangle in random indexed color
                color = new TextColor.Indexed(random.nextInt(256));
            }
        }

        TerminalPosition p1;
        TerminalPosition p2;
        TerminalPosition p3;
        if(rotating) {
            screen.clear();
            double triangleSize = 15.0;
            int x0 = (size.getColumns() / 2) + (int) (Math.cos(rad) * triangleSize);
            int y0 = (size.getRows() / 2) + (int) (Math.sin(rad) * triangleSize);
            int x1 = (size.getColumns() / 2) + (int) (Math.cos(rad + oneThirdOf2PI) * triangleSize);
            int y1 = (size.getRows() / 2) + (int) (Math.sin(rad + oneThirdOf2PI) * triangleSize);
            int x2 = (size.getColumns() / 2) + (int) (Math.cos(rad + twoThirdsOf2PI) * triangleSize);
            int y2 = (size.getRows() / 2) + (int) (Math.sin(rad + twoThirdsOf2PI) * triangleSize);
            p1 = new TerminalPosition(x0, y0);
            p2 = new TerminalPosition(x1, y1);
            p3 = new TerminalPosition(x2, y2);
            rad += Math.PI / 90.0;
        }
        else {
            p1 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
            p2 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
            p3 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
        }

        graphics.setBackgroundColor(color);
        if(useFilled) {
            graphics.fillTriangle(p1, p2, p3, ' ');
        }
        else {
            graphics.drawTriangle(p1, p2, p3, ' ');
        }
        screen.refresh(Screen.RefreshType.DELTA);
        if(slow) {
            Thread.sleep(500);
        }
    }
    screen.stopScreen();
}
 
Example 10
Source File: ScreenRectangleTest.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    boolean useAnsiColors = false;
    boolean useFilled = false;
    boolean slow = false;
    for(String arg: args) {
        if(arg.equals("--ansi-colors")) {
            useAnsiColors = true;
        }
        if(arg.equals("--filled")) {
            useFilled = true;
        }
        if(arg.equals("--slow")) {
            slow = true;
        }
    }
    Screen screen = new TestTerminalFactory(args).createScreen();
    screen.startScreen();

    TextGraphics textGraphics = new ScreenTextGraphics(screen);
    Random random = new Random();

    long startTime = System.currentTimeMillis();
    while(System.currentTimeMillis() - startTime < 1000 * 20) {
        KeyStroke keyStroke = screen.pollInput();
        if(keyStroke != null &&
                (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) {
            break;
        }
        screen.doResizeIfNecessary();
        TerminalSize size = textGraphics.getSize();
        TextColor color;
        if(useAnsiColors) {
            color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)];
        }
        else {
            //Draw a rectangle in random indexed color
            color = new TextColor.Indexed(random.nextInt(256));
        }

        TerminalPosition topLeft = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
        TerminalSize rectangleSize = new TerminalSize(random.nextInt(size.getColumns() - topLeft.getColumn()), random.nextInt(size.getRows() - topLeft.getRow()));

        textGraphics.setBackgroundColor(color);
        if(useFilled) {
            textGraphics.fillRectangle(topLeft, rectangleSize, ' ');
        }
        else {
            textGraphics.drawRectangle(topLeft, rectangleSize, ' ');
        }
        screen.refresh(Screen.RefreshType.DELTA);
        if(slow) {
            Thread.sleep(500);
        }
    }
    screen.stopScreen();
}
 
Example 11
Source File: ScreenLineTest.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    boolean useAnsiColors = false;
    boolean slow = false;
    boolean circle = false;
    for(String arg: args) {
        if(arg.equals("--ansi-colors")) {
            useAnsiColors = true;
        }
        if(arg.equals("--slow")) {
            slow = true;
        }
        if(arg.equals("--circle")) {
            circle = true;
        }
    }
    Screen screen = new TestTerminalFactory(args).createScreen();
    screen.startScreen();

    TextGraphics textGraphics = new ScreenTextGraphics(screen);
    Random random = new Random();
    while(true) {
        KeyStroke keyStroke = screen.pollInput();
        if(keyStroke != null &&
                (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) {
            break;
        }
        screen.doResizeIfNecessary();
        TerminalSize size = textGraphics.getSize();
        TextColor color;
        if(useAnsiColors) {
            color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)];
        }
        else {
            //Draw a rectangle in random indexed color
            color = new TextColor.Indexed(random.nextInt(256));
        }

        TerminalPosition p1;
        TerminalPosition p2;
        if(circle) {
            p1 = new TerminalPosition(size.getColumns() / 2, size.getRows() / 2);
            if(CIRCLE_LAST_POSITION == null) {
                CIRCLE_LAST_POSITION = new TerminalPosition(0, 0);
            }
            else if(CIRCLE_LAST_POSITION.getRow() == 0) {
                if(CIRCLE_LAST_POSITION.getColumn() < size.getColumns() - 1) {
                    CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeColumn(1);
                }
                else {
                    CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(1);
                }
            }
            else if(CIRCLE_LAST_POSITION.getRow() < size.getRows() - 1) {
                if(CIRCLE_LAST_POSITION.getColumn() == 0) {
                    CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(-1);
                }
                else {
                    CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(1);
                }
            }
            else {
                if(CIRCLE_LAST_POSITION.getColumn() > 0) {
                    CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeColumn(-1);
                }
                else {
                    CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(-1);
                }
            }
            p2 = CIRCLE_LAST_POSITION;
        }
        else {
            p1 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
            p2 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows()));
        }
        textGraphics.setBackgroundColor(color);
        textGraphics.drawLine(p1, p2, ' ');
        textGraphics.setBackgroundColor(TextColor.ANSI.BLACK);
        textGraphics.setForegroundColor(TextColor.ANSI.WHITE);
        textGraphics.putString(4, size.getRows() - 1, "P1 " + p1 + " -> P2 " + p2);
        screen.refresh(Screen.RefreshType.DELTA);
        if(slow) {
            Thread.sleep(500);
        }
    }
    screen.stopScreen();
}