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/**
026 * Represents the user pressing a key on the keyboard.
027 */
028@InterfaceAudience.Private
029public class KeyPress {
030  public enum Type {
031    Character,
032    Escape,
033    Backspace,
034    ArrowLeft,
035    ArrowRight,
036    ArrowUp,
037    ArrowDown,
038    Insert,
039    Delete,
040    Home,
041    End,
042    PageUp,
043    PageDown,
044    ReverseTab,
045    Tab,
046    Enter,
047    F1,
048    F2,
049    F3,
050    F4,
051    F5,
052    F6,
053    F7,
054    F8,
055    F9,
056    F10,
057    F11,
058    F12,
059    Unknown
060  }
061
062  private final Type type;
063  private final Character character;
064  private final boolean alt;
065  private final boolean ctrl;
066  private final boolean shift;
067
068  public KeyPress(Type type, @Nullable Character character, boolean alt, boolean ctrl,
069    boolean shift) {
070    this.type = Objects.requireNonNull(type);
071    this.character = character;
072    this.alt = alt;
073    this.ctrl = ctrl;
074    this.shift = shift;
075  }
076
077  public Type getType() {
078    return type;
079  }
080
081  @Nullable
082  public Character getCharacter() {
083    return character;
084  }
085
086  public boolean isAlt() {
087    return alt;
088  }
089
090  public boolean isCtrl() {
091    return ctrl;
092  }
093
094  public boolean isShift() {
095    return shift;
096  }
097
098  @Override
099  public String toString() {
100    return "KeyPress{" +
101      "type=" + type +
102      ", character=" + escape(character) +
103      ", alt=" + alt +
104      ", ctrl=" + ctrl +
105      ", shift=" + shift +
106      '}';
107  }
108
109  private String escape(Character character) {
110    if (character == null) {
111      return "null";
112    }
113
114    switch (character) {
115      case '\n':
116        return "\\n";
117
118      case '\b':
119        return "\\b";
120
121      case '\t':
122        return "\\t";
123
124      default:
125        return character.toString();
126    }
127  }
128}