com.googlecode.lanterna.input.KeyStroke Java Examples

The following examples show how to use com.googlecode.lanterna.input.KeyStroke. 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: 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 #2
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 #3
Source File: GraphicalTerminalImplementation.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void keyTyped(KeyEvent e) {
    char character = e.getKeyChar();
    boolean altDown = (e.getModifiersEx() & InputEvent.ALT_DOWN_MASK) != 0;
    boolean ctrlDown = (e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0;
    boolean shiftDown = (e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) != 0;

    if(!TYPED_KEYS_TO_IGNORE.contains(character)) {
        //We need to re-adjust alphabet characters if ctrl was pressed, just like for the AnsiTerminal
        if(ctrlDown && character > 0 && character < 0x1a) {
            character = (char) ('a' - 1 + character);
            if(shiftDown) {
                character = Character.toUpperCase(character);
            }
        }

        // Check if clipboard is avavilable and this was a paste (ctrl + shift + v) before
        // adding the key to the input queue
        if(!altDown && ctrlDown && shiftDown && character == 'V' && deviceConfiguration.isClipboardAvailable()) {
            pasteClipboardContent();
        }
        else {
            keyQueue.add(new KeyStroke(character, ctrlDown, altDown, shiftDown));
        }
    }
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: RadioBoxList.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)) {
        setCheckedIndex(getSelectedIndex());
    } 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);
        }
        
        // includes mouse drag
        int existingIndex = getSelectedIndex();
        int newIndex = getIndexByMouseAction(mouseAction);
        if (existingIndex != newIndex || !isFocused()) {
            Result result = super.handleKeyStroke(keyStroke);
            setCheckedIndex(getSelectedIndex());
            return result;
        }
        setCheckedIndex(getSelectedIndex());
        return Result.HANDLED;
    }
    return super.handleKeyStroke(keyStroke);
}
 
Example #10
Source File: AWTTerminalImplementation.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke readInput() {
    if(EventQueue.isDispatchThread()) {
        throw new UnsupportedOperationException("Cannot call SwingTerminal.readInput() on the AWT thread");
    }
    return super.readInput();
}
 
Example #11
Source File: SwingTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void processInputMethodEvent(InputMethodEvent e) {
    AttributedCharacterIterator iterator = e.getText();
    for(int i = 0; i < e.getCommittedCharacterCount(); i++) {
        terminalImplementation.addInput(new KeyStroke(iterator.current(), false, false));
        iterator.next();
    }
}
 
Example #12
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 #13
Source File: WindowsTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected KeyDecodingProfile getDefaultKeyDecodingProfile() {
	ArrayList<CharacterPattern> keyDecodingProfile = new ArrayList<CharacterPattern>();
	// handle Key Code 13 as ENTER
	keyDecodingProfile.add(new BasicCharacterPattern(new KeyStroke(KeyType.Enter), '\r'));
	// handle everything else as per default
	keyDecodingProfile.addAll(super.getDefaultKeyDecodingProfile().getPatterns());
	return () -> keyDecodingProfile;
}
 
Example #14
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 #15
Source File: GraphicalTerminalImplementation.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
synchronized void onDestroyed() {
    stopBlinkTimer();
    enableInput = false;

    // If a thread is blocked, waiting on something in the keyQueue...
    keyQueue.add(new KeyStroke(KeyType.EOF));
}
 
Example #16
Source File: DefaultVirtualTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public synchronized KeyStroke readInput() {
    try {
        return inputQueue.take();
    }
    catch(InterruptedException e) {
        throw new RuntimeException("Unexpected interrupt", e);
    }
}
 
Example #17
Source File: ComboBox.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(isReadOnly()) {
        return handleReadOnlyCBKeyStroke(keyStroke);
    }
    else {
        return handleEditableCBKeyStroke(keyStroke);
    }
}
 
Example #18
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 #19
Source File: VirtualScreen.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
private KeyStroke filter(KeyStroke keyStroke) throws IOException {
    if(keyStroke == null) {
        return null;
    }
    else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowLeft) {
        if(viewportTopLeft.getColumn() > 0) {
            viewportTopLeft = viewportTopLeft.withRelativeColumn(-1);
            refresh();
            return null;
        }
    }
    else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowRight) {
        if(viewportTopLeft.getColumn() + viewportSize.getColumns() < getTerminalSize().getColumns()) {
            viewportTopLeft = viewportTopLeft.withRelativeColumn(1);
            refresh();
            return null;
        }
    }
    else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowUp) {
        if(viewportTopLeft.getRow() > 0) {
            viewportTopLeft = viewportTopLeft.withRelativeRow(-1);
            realScreen.scrollLines(0,viewportSize.getRows()-1,-1);
            refresh();
            return null;
        }
    }
    else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowDown) {
        if(viewportTopLeft.getRow() + viewportSize.getRows() < getTerminalSize().getRows()) {
            viewportTopLeft = viewportTopLeft.withRelativeRow(1);
            realScreen.scrollLines(0,viewportSize.getRows()-1,1);
            refresh();
            return null;
        }
    }
    return keyStroke;
}
 
Example #20
Source File: AWTTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void processInputMethodEvent(InputMethodEvent e) {
    AttributedCharacterIterator iterator = e.getText();
    for(int i = 0; i < e.getCommittedCharacterCount(); i++) {
        terminalImplementation.addInput(new KeyStroke(iterator.current(), false, false));
        iterator.next();
    }
}
 
Example #21
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 #22
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 #23
Source File: IOSafeTerminalAdapter.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke pollInput() {
    try {
        return backend.pollInput();
    }
    catch(IOException e) {
        exceptionHandler.onException(e);
    }
    return null;
}
 
Example #24
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 #25
Source File: UnixLikeTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke readInput() throws IOException {
    //Check if we have ctrl+c coming
    KeyStroke key = super.readInput();
    isCtrlC(key);
    return key;
}
 
Example #26
Source File: UnixLikeTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke pollInput() throws IOException {
    //Check if we have ctrl+c coming
    KeyStroke key = super.pollInput();
    isCtrlC(key);
    return key;
}
 
Example #27
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 #28
Source File: IOSafeTerminalAdapter.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public KeyStroke readInput() {
    try {
        return backend.readInput();
    }
    catch(IOException e) {
        exceptionHandler.onException(e);
    }
    return null;
}
 
Example #29
Source File: AbstractInteractableComponent.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean isMouseActivationStroke(KeyStroke keyStroke) {
    boolean isMouseActivation = false;
    if (keyStroke instanceof MouseAction) {
        MouseAction action = (MouseAction)keyStroke;
        isMouseActivation = action.getActionType() == MouseActionType.CLICK_DOWN;
    }
    
    return isMouseActivation;
}
 
Example #30
Source File: Button.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 (isActivationStroke(keyStroke)) {
        triggerActions();
        return Result.HANDLED;
    }
    return super.handleKeyStroke(keyStroke);
}