001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.terminal;
019
020import edu.umd.cs.findbugs.annotations.Nullable;
021import java.util.Objects;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Represents the user pressing a key on the keyboard.
026 */
027@InterfaceAudience.Private
028public class KeyPress {
029  public enum Type {
030    Character,
031    Escape,
032    Backspace,
033    ArrowLeft,
034    ArrowRight,
035    ArrowUp,
036    ArrowDown,
037    Insert,
038    Delete,
039    Home,
040    End,
041    PageUp,
042    PageDown,
043    ReverseTab,
044    Tab,
045    Enter,
046    F1,
047    F2,
048    F3,
049    F4,
050    F5,
051    F6,
052    F7,
053    F8,
054    F9,
055    F10,
056    F11,
057    F12,
058    Unknown
059  }
060
061  private final Type type;
062  private final Character character;
063  private final boolean alt;
064  private final boolean ctrl;
065  private final boolean shift;
066
067  public KeyPress(Type type, @Nullable Character character, boolean alt, boolean ctrl,
068    boolean shift) {
069    this.type = Objects.requireNonNull(type);
070    this.character = character;
071    this.alt = alt;
072    this.ctrl = ctrl;
073    this.shift = shift;
074  }
075
076  public Type getType() {
077    return type;
078  }
079
080  @Nullable
081  public Character getCharacter() {
082    return character;
083  }
084
085  public boolean isAlt() {
086    return alt;
087  }
088
089  public boolean isCtrl() {
090    return ctrl;
091  }
092
093  public boolean isShift() {
094    return shift;
095  }
096
097  @Override
098  public String toString() {
099    return "KeyPress{" + "type=" + type + ", character=" + escape(character) + ", alt=" + alt
100      + ", ctrl=" + ctrl + ", shift=" + shift + '}';
101  }
102
103  private String escape(Character character) {
104    if (character == null) {
105      return "null";
106    }
107
108    switch (character) {
109      case '\n':
110        return "\\n";
111
112      case '\b':
113        return "\\b";
114
115      case '\t':
116        return "\\t";
117
118      default:
119        return character.toString();
120    }
121  }
122}