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

The following examples show how to use com.googlecode.lanterna.input.KeyType#EOF . 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: AbstractTextGUI.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public synchronized boolean processInput() throws IOException {
    boolean gotInput = false;
    KeyStroke keyStroke = readKeyStroke();
    if(keyStroke != null) {
        gotInput = true;
        do {
            if (keyStroke.getKeyType() == KeyType.EOF) {
                throw new EOFException();
            }
            boolean handled = handleInput(keyStroke);
            if(!handled) {
                handled = fireUnhandledKeyStroke(keyStroke);
            }
            dirty = handled || dirty;
            keyStroke = pollInput();
        } while(keyStroke != null);
    }
    return gotInput;
}
 
Example 2
Source File: TerminalScreen.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
public synchronized void stopScreen(boolean flushInput) throws IOException {
    if(!isStarted) {
        return;
    }

    if (flushInput) {
        //Drain the input queue
        KeyStroke keyStroke;
        do {
            keyStroke = pollInput();
        }
        while(keyStroke != null && keyStroke.getKeyType() != KeyType.EOF);
    }

    getTerminal().exitPrivateMode();
    isStarted = false;
}
 
Example 3
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 4
Source File: MultiWindowTextGUI.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected synchronized KeyStroke readKeyStroke() throws IOException {
    KeyStroke keyStroke = super.pollInput();
    if(hadWindowAtSomePoint && eofWhenNoWindows && keyStroke == null && windows.isEmpty()) {
        return new KeyStroke(KeyType.EOF);
    }
    else if(keyStroke != null) {
        return keyStroke;
    }
    else {
        return super.readKeyStroke();
    }
}
 
Example 5
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 6
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 7
Source File: GraphicalTerminalImplementation.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke pollInput() {
    if(!enableInput) {
        return new KeyStroke(KeyType.EOF);
    }
    return keyQueue.poll();
}
 
Example 8
Source File: GraphicalTerminalImplementation.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke readInput() {
    // Synchronize on keyQueue here so only one thread is inside keyQueue.take()
    synchronized(keyQueue) {
        if(!enableInput) {
            return new KeyStroke(KeyType.EOF);
        }
        try {
            return keyQueue.take();
        }
        catch(InterruptedException ignore) {
            throw new RuntimeException("Blocking input was interrupted");
        }
    }
}
 
Example 9
Source File: GraphicalTerminalImplementation.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void injectStringAsKeyStrokes(String string) {
    StringReader stringReader = new StringReader(string);
    InputDecoder inputDecoder = new InputDecoder(stringReader);
    inputDecoder.addProfile(new DefaultKeyDecodingProfile());
    try {
        KeyStroke keyStroke = inputDecoder.getNextCharacter(false);
        while (keyStroke != null && keyStroke.getKeyType() != KeyType.EOF) {
            keyQueue.add(keyStroke);
            keyStroke = inputDecoder.getNextCharacter(false);
        }
    }
    catch(IOException ignore) {
    }
}
 
Example 10
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 11
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 12
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 13
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();
}