TTreeView compiles
[fanfix.git] / src / jexer / event / TKeypressEvent.java
CommitLineData
df8de03f
KL
1/**
2 * Jexer - Java Text User Interface
3 *
df8de03f
KL
4 * License: LGPLv3 or later
5 *
7b5261bc
KL
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
df8de03f
KL
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
7b5261bc
KL
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
df8de03f
KL
30 */
31package jexer.event;
32
33import jexer.TKeypress;
34
35/**
36 * This class encapsulates a keyboard input event.
37 */
b299e69c 38public final class TKeypressEvent extends TInputEvent {
df8de03f
KL
39
40 /**
7b5261bc 41 * Keystroke received.
df8de03f 42 */
b299e69c 43 private TKeypress key;
df8de03f
KL
44
45 /**
b299e69c
KL
46 * Get keystroke.
47 *
48 * @return keystroke
df8de03f 49 */
b299e69c
KL
50 public TKeypress getKey() {
51 return key;
df8de03f
KL
52 }
53
54 /**
7b5261bc 55 * Public contructor.
df8de03f
KL
56 *
57 * @param key the TKeypress received
58 */
7b5261bc
KL
59 public TKeypressEvent(final TKeypress key) {
60 this.key = key;
df8de03f
KL
61 }
62
b299e69c
KL
63 /**
64 * Public constructor.
65 *
66 * @param isKey is true, this is a function key
67 * @param fnKey the function key code (only valid if isKey is true)
68 * @param ch the character (only valid if fnKey is false)
69 * @param alt if true, ALT was pressed with this keystroke
70 * @param ctrl if true, CTRL was pressed with this keystroke
71 * @param shift if true, SHIFT was pressed with this keystroke
72 */
73 public TKeypressEvent(final boolean isKey, final int fnKey, final char ch,
74 final boolean alt, final boolean ctrl, final boolean shift) {
75
76 this.key = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift);
77 }
78
79 /**
80 * Public constructor.
81 *
82 * @param key the TKeypress received
83 * @param alt if true, ALT was pressed with this keystroke
84 * @param ctrl if true, CTRL was pressed with this keystroke
85 * @param shift if true, SHIFT was pressed with this keystroke
86 */
87 public TKeypressEvent(final TKeypress key,
88 final boolean alt, final boolean ctrl, final boolean shift) {
89
7c870d89 90 this.key = new TKeypress(key.isFnKey(), key.getKeyCode(), key.getChar(),
b299e69c
KL
91 alt, ctrl, shift);
92 }
93
94 /**
95 * Comparison check. All fields must match to return true.
96 *
97 * @param rhs another TKeypressEvent or TKeypress instance
98 * @return true if all fields are equal
99 */
100 @Override
101 public boolean equals(final Object rhs) {
102 if (!(rhs instanceof TKeypressEvent)
103 && !(rhs instanceof TKeypress)
104 ) {
105 return false;
106 }
107
108 if (rhs instanceof TKeypressEvent) {
109 TKeypressEvent that = (TKeypressEvent) rhs;
110 return (key.equals(that.key)
111 && (getTime().equals(that.getTime())));
112 }
113
114 TKeypress that = (TKeypress) rhs;
115 return (key.equals(that));
116 }
117
e826b451
KL
118 /**
119 * Hashcode uses all fields in equals().
120 *
121 * @return the hash
122 */
123 @Override
124 public int hashCode() {
125 int A = 13;
126 int B = 23;
127 int hash = A;
128 hash = (B * hash) + getTime().hashCode();
129 hash = (B * hash) + key.hashCode();
130 return hash;
131 }
132
df8de03f 133 /**
7b5261bc
KL
134 * Make human-readable description of this TKeypressEvent.
135 *
136 * @return displayable String
df8de03f
KL
137 */
138 @Override
b299e69c 139 public String toString() {
7b5261bc 140 return String.format("Keypress: %s", key.toString());
df8de03f
KL
141 }
142}