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

The following examples show how to use com.googlecode.lanterna.input.KeyStroke#getKeyType() . 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: ComboBox.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Result handleReadOnlyCBKeyStroke(KeyStroke keyStroke) {
    switch(keyStroke.getKeyType()) {
        case Character:
        case Enter:
            if (isKeyboardActivationStroke(keyStroke)) {
                showPopup(keyStroke);
            }
            return super.handleKeyStroke(keyStroke);
        
        case MouseEvent:
            if (isMouseActivationStroke(keyStroke)) {
                showPopup(keyStroke);
            }
            break;
        
        default:
    }
    return super.handleKeyStroke(keyStroke);
}
 
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: 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 4
Source File: CheckBoxList.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public synchronized Result handleKeyStroke(KeyStroke keyStroke) {
    if (isKeyboardActivationStroke(keyStroke)) {
        toggleChecked(getSelectedIndex());
        return Result.HANDLED;
    } else if (keyStroke.getKeyType() == KeyType.MouseEvent) {
        MouseAction mouseAction = (MouseAction) keyStroke;
        MouseActionType actionType = mouseAction.getActionType();
        
        if (isMouseMove(keyStroke)
                || actionType == MouseActionType.CLICK_RELEASE
                || actionType == MouseActionType.SCROLL_UP
                || actionType == MouseActionType.SCROLL_DOWN) {
            return super.handleKeyStroke(keyStroke);
        }
        
        Result result = super.handleKeyStroke(keyStroke);
        int newIndex = getIndexByMouseAction(mouseAction);
        if (actionType == MouseActionType.CLICK_DOWN) {
            stateForMouseDragged = !isChecked(newIndex);
            setChecked(newIndex, stateForMouseDragged);
            minIndexForMouseDragged = newIndex;
            maxIndexForMouseDragged = newIndex;
        }
        
        minIndexForMouseDragged = Math.min(minIndexForMouseDragged, newIndex);
        maxIndexForMouseDragged = Math.max(maxIndexForMouseDragged, newIndex);
        
        if (actionType == MouseActionType.DRAG) {
            for (int i = minIndexForMouseDragged; i <= maxIndexForMouseDragged; i++) {
             setChecked(i, stateForMouseDragged);
            }
        }
        return result;
    }
    
    return super.handleKeyStroke(keyStroke);
}
 
Example 5
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 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: 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 8
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 9
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 10
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 11
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 12
Source File: Table.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Result handleKeyStroke(KeyStroke keyStroke) {
    switch(keyStroke.getKeyType()) {
        case ArrowUp:
            if(selectedRow > 0) {
                selectedRow--;
            }
            else if(escapeByArrowKey) {
                return Result.MOVE_FOCUS_UP;
            }
            break;
        case ArrowDown:
            if(selectedRow < tableModel.getRowCount() - 1) {
                selectedRow++;
            }
            else if(escapeByArrowKey) {
                return Result.MOVE_FOCUS_DOWN;
            }
            break;
        case PageUp:
            if(getRenderer().getVisibleRowsOnLastDraw() > 0 && selectedRow > 0) {
                selectedRow -= Math.min(getRenderer().getVisibleRowsOnLastDraw() - 1, selectedRow);
            }
            break;
        case PageDown:
            if(getRenderer().getVisibleRowsOnLastDraw() > 0 && selectedRow < tableModel.getRowCount() - 1) {
                int toEndDistance = tableModel.getRowCount() - 1 - selectedRow;
                selectedRow += Math.min(getRenderer().getVisibleRowsOnLastDraw() - 1, toEndDistance);
            }
            break;
        case Home:
            selectedRow = 0;
            break;
        case End:
            selectedRow = tableModel.getRowCount() - 1;
            break;
        case ArrowLeft:
            if(cellSelection && selectedColumn > 0) {
                selectedColumn--;
            }
            else if(escapeByArrowKey) {
                return Result.MOVE_FOCUS_LEFT;
            }
            break;
        case ArrowRight:
            if(cellSelection && selectedColumn < tableModel.getColumnCount() - 1) {
                selectedColumn++;
            }
            else if(escapeByArrowKey) {
                return Result.MOVE_FOCUS_RIGHT;
            }
            break;
        case Character:
        case Enter:
            if (isKeyboardActivationStroke(keyStroke)) {
                Runnable runnable = selectAction;   //To avoid synchronizing
                if(runnable != null) {
                    runnable.run();
                } else {
                    return Result.HANDLED;
                }
                break;
            } else {
                return super.handleKeyStroke(keyStroke);
            }
        case MouseEvent:
            MouseAction action = (MouseAction)keyStroke;
            MouseActionType actionType = action.getActionType();
            if (actionType == MouseActionType.MOVE) {
                // do nothing
                return Result.UNHANDLED;
            } 
            if (!isFocused()) {
                super.handleKeyStroke(keyStroke);
            }
            int mouseRow = getRowByMouseAction((MouseAction) keyStroke);
            int mouseColumn = getColumnByMouseAction((MouseAction) keyStroke);
            boolean isDifferentCell = mouseRow != selectedRow || mouseColumn != selectedColumn;
            selectedRow = mouseRow;
            selectedColumn = mouseColumn;
            if (isDifferentCell) {
                return handleKeyStroke(new KeyStroke(KeyType.Enter));
            }
            break;
        default:
            return super.handleKeyStroke(keyStroke);
    }
    invalidate();
    return Result.HANDLED;
}
 
Example 13
Source File: TextBox.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Result handleKeyStrokeReadOnly(KeyStroke keyStroke) {
    switch (keyStroke.getKeyType()) {
        case ArrowLeft:
            if(getRenderer().getViewTopLeft().getColumn() == 0 && horizontalFocusSwitching) {
                return Result.MOVE_FOCUS_LEFT;
            }
            getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeColumn(-1));
            return Result.HANDLED;
        case ArrowRight:
            if(getRenderer().getViewTopLeft().getColumn() + getSize().getColumns() == longestRow && horizontalFocusSwitching) {
                return Result.MOVE_FOCUS_RIGHT;
            }
            getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeColumn(1));
            return Result.HANDLED;
        case ArrowUp:
            if(getRenderer().getViewTopLeft().getRow() == 0 && verticalFocusSwitching) {
                return Result.MOVE_FOCUS_UP;
            }
            getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeRow(-1));
            return Result.HANDLED;
        case ArrowDown:
            if(getRenderer().getViewTopLeft().getRow() + getSize().getRows() == lines.size() && verticalFocusSwitching) {
                return Result.MOVE_FOCUS_DOWN;
            }
            getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeRow(1));
            return Result.HANDLED;
        case Home:
            getRenderer().setViewTopLeft(TerminalPosition.TOP_LEFT_CORNER);
            return Result.HANDLED;
        case End:
            getRenderer().setViewTopLeft(TerminalPosition.TOP_LEFT_CORNER.withRow(getLineCount() - getSize().getRows()));
            return Result.HANDLED;
        case PageDown:
            getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeRow(getSize().getRows()));
            return Result.HANDLED;
        case PageUp:
            getRenderer().setViewTopLeft(getRenderer().getViewTopLeft().withRelativeRow(-getSize().getRows()));
            return Result.HANDLED;
        default:
    }
    return super.handleKeyStroke(keyStroke);
}
 
Example 14
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();
}
 
Example 15
Source File: AbstractInteractableComponent.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean isMouseMove(KeyStroke keyStroke) {
    return keyStroke.getKeyType() == KeyType.MouseEvent && ((MouseAction)keyStroke).getActionType() == MouseActionType.MOVE;
}
 
Example 16
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 17
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 18
Source File: AbstractListBox.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public synchronized Result handleKeyStroke(KeyStroke keyStroke) {
    try {
        switch(keyStroke.getKeyType()) {
            case Tab:
                return Result.MOVE_FOCUS_NEXT;

            case ReverseTab:
                return Result.MOVE_FOCUS_PREVIOUS;

            case ArrowRight:
                return Result.MOVE_FOCUS_RIGHT;

            case ArrowLeft:
                return Result.MOVE_FOCUS_LEFT;

            case ArrowDown:
                if(items.isEmpty() || selectedIndex == items.size() - 1) {
                    return Result.MOVE_FOCUS_DOWN;
                }
                selectedIndex++;
                return Result.HANDLED;

            case ArrowUp:
                if(items.isEmpty() || selectedIndex == 0) {
                    return Result.MOVE_FOCUS_UP;
                }
                selectedIndex--;
                return Result.HANDLED;

            case Home:
                selectedIndex = 0;
                return Result.HANDLED;

            case End:
                selectedIndex = items.size() - 1;
                return Result.HANDLED;

            case PageUp:
                if(getSize() != null) {
                    setSelectedIndex(getSelectedIndex() - getSize().getRows());
                }
                return Result.HANDLED;

            case PageDown:
                if(getSize() != null) {
                    setSelectedIndex(getSelectedIndex() + getSize().getRows());
                }
                return Result.HANDLED;

            case Character:
                if(selectByCharacter(keyStroke.getCharacter())) {
                    return Result.HANDLED;
                }
                return Result.UNHANDLED;
            case MouseEvent:
                MouseAction mouseAction = (MouseAction) keyStroke;
                MouseActionType actionType = mouseAction.getActionType();
                if (isMouseMove(keyStroke)) {
                    takeFocus();
                    selectedIndex = getIndexByMouseAction(mouseAction);
                    return Result.HANDLED;
                }
                
                if (actionType == MouseActionType.CLICK_RELEASE) {
                    // do nothing, desired actioning has been performed already on CLICK_DOWN and DRAG
                    return Result.HANDLED;
                } else if (actionType == MouseActionType.SCROLL_UP) {
                    // relying on setSelectedIndex(index) to clip the index to valid values within range
                    setSelectedIndex(getSelectedIndex() -1);
                    return Result.HANDLED;
                } else if (actionType == MouseActionType.SCROLL_DOWN) {
                    // relying on setSelectedIndex(index) to clip the index to valid values within range
                    setSelectedIndex(getSelectedIndex() +1);
                    return Result.HANDLED;
                }
        
                selectedIndex = getIndexByMouseAction(mouseAction);
                return super.handleKeyStroke(keyStroke);
            default:
        }
        return Result.UNHANDLED;
    }
    finally {
        invalidate();
    }
}
 
Example 19
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 20
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();
}