Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / event / TKeypressEvent.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.event;
30
31 import jexer.TKeypress;
32
33 /**
34 * This class encapsulates a keyboard input event.
35 */
36 public class TKeypressEvent extends TInputEvent {
37
38 // ------------------------------------------------------------------------
39 // Variables --------------------------------------------------------------
40 // ------------------------------------------------------------------------
41
42 /**
43 * Keystroke received.
44 */
45 private TKeypress key;
46
47 // ------------------------------------------------------------------------
48 // Constructors -----------------------------------------------------------
49 // ------------------------------------------------------------------------
50
51 /**
52 * Public contructor.
53 *
54 * @param key the TKeypress received
55 */
56 public TKeypressEvent(final TKeypress key) {
57 this.key = key;
58 }
59
60 /**
61 * Public constructor.
62 *
63 * @param isKey is true, this is a function key
64 * @param fnKey the function key code (only valid if isKey is true)
65 * @param ch the character (only valid if fnKey is false)
66 * @param alt if true, ALT was pressed with this keystroke
67 * @param ctrl if true, CTRL was pressed with this keystroke
68 * @param shift if true, SHIFT was pressed with this keystroke
69 */
70 public TKeypressEvent(final boolean isKey, final int fnKey, final int ch,
71 final boolean alt, final boolean ctrl, final boolean shift) {
72
73 this.key = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift);
74 }
75
76 /**
77 * Public constructor.
78 *
79 * @param key the TKeypress received
80 * @param alt if true, ALT was pressed with this keystroke
81 * @param ctrl if true, CTRL was pressed with this keystroke
82 * @param shift if true, SHIFT was pressed with this keystroke
83 */
84 public TKeypressEvent(final TKeypress key,
85 final boolean alt, final boolean ctrl, final boolean shift) {
86
87 this.key = new TKeypress(key.isFnKey(), key.getKeyCode(), key.getChar(),
88 alt, ctrl, shift);
89 }
90
91 // ------------------------------------------------------------------------
92 // TInputEvent ------------------------------------------------------------
93 // ------------------------------------------------------------------------
94
95 /**
96 * Comparison check. All fields must match to return true.
97 *
98 * @param rhs another TKeypressEvent or TKeypress instance
99 * @return true if all fields are equal
100 */
101 @Override
102 public boolean equals(final Object rhs) {
103 if (!(rhs instanceof TKeypressEvent)
104 && !(rhs instanceof TKeypress)
105 ) {
106 return false;
107 }
108
109 if (rhs instanceof TKeypressEvent) {
110 TKeypressEvent that = (TKeypressEvent) rhs;
111 return (key.equals(that.key)
112 && (getTime().equals(that.getTime())));
113 }
114
115 TKeypress that = (TKeypress) rhs;
116 return (key.equals(that));
117 }
118
119 /**
120 * Hashcode uses all fields in equals().
121 *
122 * @return the hash
123 */
124 @Override
125 public int hashCode() {
126 int A = 13;
127 int B = 23;
128 int hash = A;
129 hash = (B * hash) + getTime().hashCode();
130 hash = (B * hash) + key.hashCode();
131 return hash;
132 }
133
134 /**
135 * Make human-readable description of this TKeypressEvent.
136 *
137 * @return displayable String
138 */
139 @Override
140 public String toString() {
141 return String.format("Keypress: %s", key.toString());
142 }
143
144 // ------------------------------------------------------------------------
145 // TKeypressEvent ---------------------------------------------------------
146 // ------------------------------------------------------------------------
147
148 /**
149 * Get keystroke.
150 *
151 * @return keystroke
152 */
153 public TKeypress getKey() {
154 return key;
155 }
156
157 /**
158 * Create a duplicate instance.
159 *
160 * @return duplicate intance
161 */
162 public TKeypressEvent dup() {
163 TKeypressEvent keypress = new TKeypressEvent(key.dup());
164 return keypress;
165 }
166
167 }