org.fusesource.jansi.internal.WindowsSupport Java Examples

The following examples show how to use org.fusesource.jansi.internal.WindowsSupport. 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: AbstractWindowsTerminal.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(int[] input) {
    CharBuffer buffer = Encoder.toCharBuffer(input);
    char[] chars = buffer.array();
    if (WriteConsoleW(console, chars, chars.length, writtenChars, 0) == 0) {
        LOGGER.log(Level.WARNING, "Failed to write out.", WindowsSupport.getLastErrorMessage());
    }
}
 
Example #2
Source File: WindowsTerminal.java    From TorrentEngine with GNU General Public License v3.0 4 votes vote down vote up
private int getConsoleMode() {
    return WindowsSupport.getConsoleMode();
}
 
Example #3
Source File: WindowsTerminal.java    From TorrentEngine with GNU General Public License v3.0 4 votes vote down vote up
private void setConsoleMode(int mode) {
    WindowsSupport.setConsoleMode(mode);
}
 
Example #4
Source File: WindowsTerminal.java    From TorrentEngine with GNU General Public License v3.0 4 votes vote down vote up
private byte[] readConsoleInput() {
    // XXX does how many events to read in one call matter?
    INPUT_RECORD[] events = null;
    try {
        events = WindowsSupport.readConsoleInput(1);
    } catch (IOException e) {
        Log.debug("read Windows console input error: ", e);
    }
    if (events == null) {
        return new byte[0];
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < events.length; i++ ) {
        KEY_EVENT_RECORD keyEvent = events[i].keyEvent;
        //Log.trace(keyEvent.keyDown? "KEY_DOWN" : "KEY_UP", "key code:", keyEvent.keyCode, "char:", (long)keyEvent.uchar); 
        if (keyEvent.keyDown) {
            if (keyEvent.uchar > 0) {
                // support some C1 control sequences: ALT + [@-_] (and [a-z]?) => ESC <ascii>
                // http://en.wikipedia.org/wiki/C0_and_C1_control_codes#C1_set
                int altState = KEY_EVENT_RECORD.LEFT_ALT_PRESSED | KEY_EVENT_RECORD.RIGHT_ALT_PRESSED;
                if (((keyEvent.uchar >= '@' && keyEvent.uchar <= '_') || (keyEvent.uchar >= 'a' && keyEvent.uchar <= 'z'))
                    && (keyEvent.controlKeyState & altState) != 0) {
                    sb.append('\u001B'); // ESC
                }

                sb.append(keyEvent.uchar);
                continue;
            }
            // virtual keycodes: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
            // just add support for basic editing keys (no control state, no numpad keys)
            String escapeSequence = null;
            switch (keyEvent.keyCode) {
            case 0x21: // VK_PRIOR PageUp
                escapeSequence = "\u001B[5~";
                break;
            case 0x22: // VK_NEXT PageDown
                escapeSequence = "\u001B[6~";
                break;
            case 0x23: // VK_END
                escapeSequence = "\u001B[4~";
                break;
            case 0x24: // VK_HOME
                escapeSequence = "\u001B[1~";
                break;
            case 0x25: // VK_LEFT
                escapeSequence = "\u001B[D";
                break;
            case 0x26: // VK_UP
                escapeSequence = "\u001B[A";
                break;
            case 0x27: // VK_RIGHT
                escapeSequence = "\u001B[C";
                break;
            case 0x28: // VK_DOWN
                escapeSequence = "\u001B[B";
                break;
            case 0x2D: // VK_INSERT
                escapeSequence = "\u001B[2~";
                break;
            case 0x2E: // VK_DELETE
                escapeSequence = "\u001B[3~";
                break;
            default:
                break;
            }
            if (escapeSequence != null) {
                for (int k = 0; k < keyEvent.repeatCount; k++) {
                    sb.append(escapeSequence);
                }
            }
        } else {
            // key up event
            // support ALT+NumPad input method
            if (keyEvent.keyCode == 0x12/*VK_MENU ALT key*/ && keyEvent.uchar > 0) {
                sb.append(keyEvent.uchar);
            }
        }
    }
    return sb.toString().getBytes();
}
 
Example #5
Source File: WindowsTerminal.java    From TorrentEngine with GNU General Public License v3.0 4 votes vote down vote up
private int getWindowsTerminalWidth() {
    return WindowsSupport.getWindowsTerminalWidth();
}
 
Example #6
Source File: WindowsTerminal.java    From TorrentEngine with GNU General Public License v3.0 4 votes vote down vote up
private int getWindowsTerminalHeight() {
    return WindowsSupport.getWindowsTerminalHeight();
}
 
Example #7
Source File: WinSysTerminal.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@Override
protected int getConsoleMode() {
    return WindowsSupport.getConsoleMode();
}
 
Example #8
Source File: WinSysTerminal.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@Override
protected void setConsoleMode(int mode) {
    WindowsSupport.setConsoleMode(mode);
}
 
Example #9
Source File: WinSysTerminal.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
public Size getSize() {
    Size size = new Size(WindowsSupport.getWindowsTerminalWidth(),
            WindowsSupport.getWindowsTerminalHeight());
    return size;
}
 
Example #10
Source File: WinSysTerminal.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
protected byte[] readConsoleInput() {
    // XXX does how many events to read in one call matter?
    INPUT_RECORD[] events = null;
    try {
        events = WindowsSupport.readConsoleInput(1);
    } catch (IOException e) {
        LOGGER.log(Level.INFO, "read Windows terminal input error: ", e);
    }
    if (events == null) {
        return new byte[0];
    }
    StringBuilder sb = new StringBuilder();
    for (INPUT_RECORD event : events) {
        KEY_EVENT_RECORD keyEvent = event.keyEvent;
        // support some C1 control sequences: ALT + [@-_] (and [a-z]?) => ESC <ascii>
        // http://en.wikipedia.org/wiki/C0_and_C1_control_codes#C1_set
        final int altState = KEY_EVENT_RECORD.LEFT_ALT_PRESSED | KEY_EVENT_RECORD.RIGHT_ALT_PRESSED;
        // Pressing "Alt Gr" is translated to Alt-Ctrl, hence it has to be checked that Ctrl is _not_ pressed,
        // otherwise inserting of "Alt Gr" codes on non-US keyboards would yield errors
        final int ctrlState = KEY_EVENT_RECORD.LEFT_CTRL_PRESSED | KEY_EVENT_RECORD.RIGHT_CTRL_PRESSED;
        // Compute the overall alt state
        boolean isAlt = ((keyEvent.controlKeyState & altState) != 0) && ((keyEvent.controlKeyState & ctrlState) == 0);

        //Log.trace(keyEvent.keyDown? "KEY_DOWN" : "KEY_UP", "key code:", keyEvent.keyCode, "char:", (long)keyEvent.uchar);
        if (keyEvent.keyDown) {
            if (keyEvent.uchar > 0) {
                boolean shiftPressed = (keyEvent.controlKeyState & KEY_EVENT_RECORD.SHIFT_PRESSED) != 0;
                if (keyEvent.uchar == '\t' && shiftPressed) {
                    sb.append(getSequence(Capability.key_btab));
                } else {
                    if (isAlt) {
                        sb.append('\033');
                    }
                    sb.append(keyEvent.uchar);
                }
            }
            else {
                // virtual keycodes: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
                // TODO: numpad keys, modifiers
                String escapeSequence = getEscapeSequence(keyEvent.keyCode);
                if (escapeSequence != null) {
                    for (int k = 0; k < keyEvent.repeatCount; k++) {
                        if (isAlt) {
                            sb.append('\033');
                        }
                        sb.append(escapeSequence);
                    }
                }
            }
        } else {
            // key up event
            // support ALT+NumPad input method
            if (keyEvent.keyCode == 0x12/*VK_MENU ALT key*/ && keyEvent.uchar > 0) {
                sb.append(keyEvent.uchar);
            }
        }
    }
    return sb.toString().getBytes();
}