Java Code Examples for com.googlecode.lanterna.input.KeyStroke#getCharacter()

The following examples show how to use com.googlecode.lanterna.input.KeyStroke#getCharacter() . 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: TerminalResizeTest.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, IOException {
    Terminal terminal = new TestTerminalFactory(args).createTerminal();
    terminal.enterPrivateMode();
    terminal.clearScreen();
    terminal.setCursorPosition(10, 5);
    terminal.putCharacter('H');
    terminal.putCharacter('e');
    terminal.putCharacter('l');
    terminal.putCharacter('l');
    terminal.putCharacter('o');
    terminal.putCharacter('!');
    terminal.setCursorPosition(0, 0);
    terminal.flush();
    terminal.addResizeListener(new TerminalResizeTest());

    while(true) {
        KeyStroke key = terminal.pollInput();
        if(key == null || key.getCharacter() != 'q') {
            Thread.sleep(1);
        }
        else {
            break;
        }
    }
    terminal.exitPrivateMode();
}
 
Example 2
Source File: UnixLikeTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void isCtrlC(KeyStroke key) throws IOException {
    if(key != null
            && terminalCtrlCBehaviour == CtrlCBehaviour.CTRL_C_KILLS_APPLICATION
            && key.getCharacter() != null
            && key.getCharacter() == 'c'
            && !key.isAltDown()
            && key.isCtrlDown()) {

        if (isInPrivateMode()) {
            exitPrivateMode();
        }
        System.exit(1);
    }
}
 
Example 3
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 4
Source File: ComboBox.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Result handleEditableCBKeyStroke(KeyStroke keyStroke) {
    //First check if we are in drop-down focused mode, treat keystrokes a bit differently then
    if(isDropDownFocused()) {
        switch(keyStroke.getKeyType()) {
            case ReverseTab:
            case ArrowLeft:
                dropDownFocused = false;
                textInputPosition = text.length();
                return Result.HANDLED;

            //The rest we can process in the same way as with read-only combo boxes when we are in drop-down focused mode
            default:
                return handleReadOnlyCBKeyStroke(keyStroke);
        }
    }

    switch(keyStroke.getKeyType()) {
        case Character:
            text = text.substring(0, textInputPosition) + keyStroke.getCharacter() + text.substring(textInputPosition);
            textInputPosition++;
            return Result.HANDLED;

        case Tab:
            dropDownFocused = true;
            return Result.HANDLED;

        case Backspace:
            if(textInputPosition > 0) {
                text = text.substring(0, textInputPosition - 1) + text.substring(textInputPosition);
                textInputPosition--;
            }
            return Result.HANDLED;

        case Delete:
            if(textInputPosition < text.length()) {
                text = text.substring(0, textInputPosition) + text.substring(textInputPosition + 1);
            }
            return Result.HANDLED;

        case ArrowLeft:
            if(textInputPosition > 0) {
                textInputPosition--;
            }
            else {
                return Result.MOVE_FOCUS_LEFT;
            }
            return Result.HANDLED;

        case ArrowRight:
            if(textInputPosition < text.length()) {
                textInputPosition++;
            }
            else {
                dropDownFocused = true;
                return Result.HANDLED;
            }
            return Result.HANDLED;

        case ArrowDown:
            if(selectedIndex < items.size() - 1) {
                setSelectedIndex(selectedIndex + 1);
            }
            return Result.HANDLED;

        case ArrowUp:
            if(selectedIndex > 0) {
                setSelectedIndex(selectedIndex - 1);
            }
            return Result.HANDLED;

        default:
    }
    return super.handleKeyStroke(keyStroke);
}
 
Example 5
Source File: AbstractInteractableComponent.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean isKeyboardActivationStroke(KeyStroke keyStroke) {
    boolean isKeyboardActivation = (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ') || keyStroke.getKeyType() == KeyType.Enter;
    
    return isFocused() && isKeyboardActivation;
}
 
Example 6
Source File: SimpleScreenTest.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 {
    Terminal terminal = new TestTerminalFactory(args).createTerminal();
    Screen screen = new TerminalScreen(terminal);
    screen.startScreen();
    screen.refresh();

    TextGraphics textGraphics = screen.newTextGraphics();

    int foregroundCycle = 1;
    int backgroundCycle = 0;

    mainLoop:
    while(true) {
        KeyStroke keyStroke = screen.readInput();
        switch(keyStroke.getKeyType()) {
            case EOF:
            case Escape:
                break mainLoop;

            case ArrowUp:
                screen.setCursorPosition(screen.getCursorPosition().withRelativeRow(-1));
                break;

            case ArrowDown:
                screen.setCursorPosition(screen.getCursorPosition().withRelativeRow(1));
                break;

            case ArrowLeft:
                screen.setCursorPosition(screen.getCursorPosition().withRelativeColumn(-1));
                break;

            case ArrowRight:
                screen.setCursorPosition(screen.getCursorPosition().withRelativeColumn(1));
                break;

            case Character:
                if(keyStroke.isCtrlDown()) {
                    switch(keyStroke.getCharacter()) {
                        case 'k':
                            screen.setCharacter(screen.getCursorPosition(), new TextCharacter('桜', COLORS_TO_CYCLE[foregroundCycle], COLORS_TO_CYCLE[backgroundCycle]));
                            screen.setCursorPosition(screen.getCursorPosition().withRelativeColumn(2));
                            break;

                        case 'f':
                            foregroundCycle++;
                            if(foregroundCycle >= COLORS_TO_CYCLE.length) {
                                foregroundCycle = 0;
                            }
                            break;

                        case 'b':
                            backgroundCycle++;
                            if(backgroundCycle >= COLORS_TO_CYCLE.length) {
                                backgroundCycle = 0;
                            }
                            break;
                    }
                    if(COLORS_TO_CYCLE[foregroundCycle] != TextColor.ANSI.BLACK) {
                        textGraphics.setBackgroundColor(TextColor.ANSI.BLACK);
                    }
                    else {
                        textGraphics.setBackgroundColor(TextColor.ANSI.WHITE);
                    }
                    textGraphics.setForegroundColor(COLORS_TO_CYCLE[foregroundCycle]);
                    textGraphics.putString(0, screen.getTerminalSize().getRows() - 2, "Foreground color");

                    if(COLORS_TO_CYCLE[backgroundCycle] != TextColor.ANSI.BLACK) {
                        textGraphics.setBackgroundColor(TextColor.ANSI.BLACK);
                    }
                    else {
                        textGraphics.setBackgroundColor(TextColor.ANSI.WHITE);
                    }
                    textGraphics.setForegroundColor(COLORS_TO_CYCLE[backgroundCycle]);
                    textGraphics.putString(0, screen.getTerminalSize().getRows() - 1, "Background color");
                }
                else {
                    screen.setCharacter(screen.getCursorPosition(), new TextCharacter(keyStroke.getCharacter(), COLORS_TO_CYCLE[foregroundCycle], COLORS_TO_CYCLE[backgroundCycle]));
                    screen.setCursorPosition(screen.getCursorPosition().withRelativeColumn(1));
                    break;
                }
            default:
        }

        screen.refresh();
    }

    screen.stopScreen();
}