Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
df8de03f KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
df8de03f | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
df8de03f | 7 | * |
e16dda65 KL |
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: | |
df8de03f | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
df8de03f | 17 | * |
e16dda65 KL |
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. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
df8de03f KL |
28 | */ |
29 | package jexer.event; | |
30 | ||
31 | import jexer.TKeypress; | |
32 | ||
33 | /** | |
34 | * This class encapsulates a keyboard input event. | |
35 | */ | |
051e2913 | 36 | public class TKeypressEvent extends TInputEvent { |
df8de03f | 37 | |
615a0d99 KL |
38 | // ------------------------------------------------------------------------ |
39 | // Variables -------------------------------------------------------------- | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
df8de03f | 42 | /** |
7b5261bc | 43 | * Keystroke received. |
df8de03f | 44 | */ |
b299e69c | 45 | private TKeypress key; |
df8de03f | 46 | |
615a0d99 KL |
47 | // ------------------------------------------------------------------------ |
48 | // Constructors ----------------------------------------------------------- | |
49 | // ------------------------------------------------------------------------ | |
df8de03f KL |
50 | |
51 | /** | |
7b5261bc | 52 | * Public contructor. |
df8de03f KL |
53 | * |
54 | * @param key the TKeypress received | |
55 | */ | |
7b5261bc KL |
56 | public TKeypressEvent(final TKeypress key) { |
57 | this.key = key; | |
df8de03f KL |
58 | } |
59 | ||
b299e69c KL |
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 | */ | |
afdec5e9 | 70 | public TKeypressEvent(final boolean isKey, final int fnKey, final int ch, |
b299e69c KL |
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 | ||
7c870d89 | 87 | this.key = new TKeypress(key.isFnKey(), key.getKeyCode(), key.getChar(), |
b299e69c KL |
88 | alt, ctrl, shift); |
89 | } | |
90 | ||
615a0d99 KL |
91 | // ------------------------------------------------------------------------ |
92 | // TInputEvent ------------------------------------------------------------ | |
93 | // ------------------------------------------------------------------------ | |
3e074355 | 94 | |
b299e69c KL |
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 | ||
e826b451 KL |
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 | ||
df8de03f | 134 | /** |
7b5261bc KL |
135 | * Make human-readable description of this TKeypressEvent. |
136 | * | |
137 | * @return displayable String | |
df8de03f KL |
138 | */ |
139 | @Override | |
b299e69c | 140 | public String toString() { |
7b5261bc | 141 | return String.format("Keypress: %s", key.toString()); |
df8de03f | 142 | } |
615a0d99 KL |
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 | ||
df8de03f | 167 | } |