private void generatorThread() {
    while (!stopThreads.get()) {
      Character ch;
      try {
        ch = inputCharacterQueue.poll(100, TimeUnit.MILLISECONDS);
      } catch (InterruptedException ignored) {
        continue;
      }

      if (ch == null) {
        if (parseState == ParseState.ESCAPE) {
          offer(new KeyPress(KeyPress.Type.Escape, null, false, false, false));
          initState();
        } else if (parseState != ParseState.START) {
          offer(new KeyPress(KeyPress.Type.Unknown, null, false, false, false));
          initState();
        }
        continue;
      }

      if (parseState == ParseState.START) {
        if (ch == 0x1B) {
          parseState = ParseState.ESCAPE;
          continue;
        }

        switch (ch) {
          case '\n':
          case '\r':
            offer(new KeyPress(KeyPress.Type.Enter, '\n', false, false, false));
            continue;

          case 0x08:
          case 0x7F:
            offer(new KeyPress(KeyPress.Type.Backspace, '\b', false, false, false));
            continue;

          case '\t':
            offer(new KeyPress(KeyPress.Type.Tab, '\t', false, false, false));
            continue;

          default:
            // Do nothing
            break;
        }

        if (ch < 32) {
          ctrlAndCharacter(ch);
          continue;
        }

        if (isPrintableChar(ch)) {
          // Normal character
          offer(new KeyPress(KeyPress.Type.Character, ch, false, false, false));
          continue;
        }

        offer(new KeyPress(KeyPress.Type.Unknown, null, false, false, false));
        continue;
      }

      if (parseState == ParseState.ESCAPE) {
        if (ch == 0x1B) {
          offer(new KeyPress(KeyPress.Type.Escape, null, false, false, false));
          continue;
        }

        if (ch < 32 && ch != 0x08) {
          ctrlAltAndCharacter(ch);
          initState();
          continue;
        } else if (ch == 0x7F || ch == 0x08) {
          offer(new KeyPress(KeyPress.Type.Backspace, '\b', false, false, false));
          initState();
          continue;
        }

        if (ch == '[' || ch == 'O') {
          parseState = ParseState.ESCAPE_SEQUENCE_PARAM1;
          continue;
        }

        if (isPrintableChar(ch)) {
          // Alt and character
          offer(new KeyPress(KeyPress.Type.Character, ch, true, false, false));
          initState();
          continue;
        }

        offer(new KeyPress(KeyPress.Type.Escape, null, false, false, false));
        offer(new KeyPress(KeyPress.Type.Unknown, null, false, false, false));
        initState();
        continue;
      }

      escapeSequenceCharacter(ch);
    }
  }
This generates {@link KeyPress} objects from the given input stream and offers them to the given
queue.
hbase-hbtop/src/main/java/org/apache/hadoop/hbase/hbtop/terminal/impl/KeyPressGenerator.java