e49e43ebb16cea29875751a3c86919e04827be2e
[nikiroo-utils.git] / src / jexer / event / TKeypressEvent.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
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.
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
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.event;
32
33 import jexer.TKeypress;
34
35 /**
36 * This class encapsulates a keyboard input event.
37 */
38 public final class TKeypressEvent extends TInputEvent {
39
40 /**
41 * Keystroke received.
42 */
43 private TKeypress key;
44
45 /**
46 * Get keystroke.
47 *
48 * @return keystroke
49 */
50 public TKeypress getKey() {
51 return key;
52 }
53
54 /**
55 * Public contructor.
56 *
57 * @param key the TKeypress received
58 */
59 public TKeypressEvent(final TKeypress key) {
60 this.key = key;
61 }
62
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
90 this.key = new TKeypress(key.isFnKey(), key.getKeyCode(), key.getChar(),
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
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
133 /**
134 * Make human-readable description of this TKeypressEvent.
135 *
136 * @return displayable String
137 */
138 @Override
139 public String toString() {
140 return String.format("Keypress: %s", key.toString());
141 }
142 }