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

The following examples show how to use com.googlecode.lanterna.input.KeyStroke#isCtrlDown() . 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: AbstractInteractableComponent.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * This method can be overridden to handle various user input (mostly from the keyboard) when this component is in
 * focus. The input method from the interface, {@code handleInput(..)} is final in
 * {@code AbstractInteractableComponent} to ensure the input filter is properly handled. If the filter decides that
 * this event should be processed, it will call this method.
 * @param keyStroke What input was entered by the user
 * @return Result of processing the key-stroke
 */
protected Result handleKeyStroke(KeyStroke keyStroke) {
    // Skip the keystroke if ctrl, alt or shift was down
    if(!keyStroke.isAltDown() && !keyStroke.isCtrlDown() && !keyStroke.isShiftDown()) {
        switch(keyStroke.getKeyType()) {
            case ArrowDown:
                return Result.MOVE_FOCUS_DOWN;
            case ArrowLeft:
                return Result.MOVE_FOCUS_LEFT;
            case ArrowRight:
                return Result.MOVE_FOCUS_RIGHT;
            case ArrowUp:
                return Result.MOVE_FOCUS_UP;
            case Tab:
                return Result.MOVE_FOCUS_NEXT;
            case ReverseTab:
                return Result.MOVE_FOCUS_PREVIOUS;
            case MouseEvent:
                if (isMouseMove(keyStroke)) {
                    // do nothing
                    return Result.UNHANDLED;
                }
                getBasePane().setFocusedInteractable(this);
                return Result.HANDLED;
            default:
        }
    }
    return Result.UNHANDLED;
}
 
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: MultiWindowManagerTest.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean handleInput(KeyStroke key) {
    boolean handled = super.handleInput(key);
    if(!handled) {
        switch(key.getKeyType()) {
            case ArrowDown:
                if(key.isAltDown()) {
                    setPosition(getPosition().withRelativeRow(1));
                }
                else if(key.isCtrlDown()) {
                    setFixedSize(getSize().withRelativeRows(1));
                    labelUnlockWindow.setText("false");
                }
                handled = true;
                break;
            case ArrowLeft:
                if(key.isAltDown()) {
                    setPosition(getPosition().withRelativeColumn(-1));
                }
                else if(key.isCtrlDown() && getSize().getColumns() > 1) {
                    setFixedSize(getSize().withRelativeColumns(-1));
                    labelUnlockWindow.setText("false");
                }
                handled = true;
                break;
            case ArrowRight:
                if(key.isAltDown()) {
                    setPosition(getPosition().withRelativeColumn(1));
                }
                else if(key.isCtrlDown()) {
                    setFixedSize(getSize().withRelativeColumns(1));
                    labelUnlockWindow.setText("false");
                }
                handled = true;
                break;
            case ArrowUp:
                if(key.isAltDown()) {
                    setPosition(getPosition().withRelativeRow(-1));
                }
                else if(key.isCtrlDown() && getSize().getRows() > 1) {
                    setFixedSize(getSize().withRelativeRows(-1));
                    labelUnlockWindow.setText("false");
                }
                handled = true;
                break;
        }
    }
    return handled;
}
 
Example 4
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();
}