LICENSE CHANGED TO MIT
[nikiroo-utils.git] / src / jexer / event / TKeypressEvent.java
CommitLineData
daa4106c 1/*
df8de03f
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
df8de03f 5 *
e16dda65 6 * Copyright (C) 2016 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 */
29package jexer.event;
30
31import jexer.TKeypress;
32
33/**
34 * This class encapsulates a keyboard input event.
35 */
b299e69c 36public final class TKeypressEvent extends TInputEvent {
df8de03f
KL
37
38 /**
7b5261bc 39 * Keystroke received.
df8de03f 40 */
b299e69c 41 private TKeypress key;
df8de03f
KL
42
43 /**
b299e69c
KL
44 * Get keystroke.
45 *
46 * @return keystroke
df8de03f 47 */
b299e69c
KL
48 public TKeypress getKey() {
49 return key;
df8de03f
KL
50 }
51
52 /**
7b5261bc 53 * Public contructor.
df8de03f
KL
54 *
55 * @param key the TKeypress received
56 */
7b5261bc
KL
57 public TKeypressEvent(final TKeypress key) {
58 this.key = key;
df8de03f
KL
59 }
60
b299e69c
KL
61 /**
62 * Public constructor.
63 *
64 * @param isKey is true, this is a function key
65 * @param fnKey the function key code (only valid if isKey is true)
66 * @param ch the character (only valid if fnKey is false)
67 * @param alt if true, ALT was pressed with this keystroke
68 * @param ctrl if true, CTRL was pressed with this keystroke
69 * @param shift if true, SHIFT was pressed with this keystroke
70 */
71 public TKeypressEvent(final boolean isKey, final int fnKey, final char ch,
72 final boolean alt, final boolean ctrl, final boolean shift) {
73
74 this.key = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift);
75 }
76
77 /**
78 * Public constructor.
79 *
80 * @param key the TKeypress received
81 * @param alt if true, ALT was pressed with this keystroke
82 * @param ctrl if true, CTRL was pressed with this keystroke
83 * @param shift if true, SHIFT was pressed with this keystroke
84 */
85 public TKeypressEvent(final TKeypress key,
86 final boolean alt, final boolean ctrl, final boolean shift) {
87
7c870d89 88 this.key = new TKeypress(key.isFnKey(), key.getKeyCode(), key.getChar(),
b299e69c
KL
89 alt, ctrl, shift);
90 }
91
92 /**
93 * Comparison check. All fields must match to return true.
94 *
95 * @param rhs another TKeypressEvent or TKeypress instance
96 * @return true if all fields are equal
97 */
98 @Override
99 public boolean equals(final Object rhs) {
100 if (!(rhs instanceof TKeypressEvent)
101 && !(rhs instanceof TKeypress)
102 ) {
103 return false;
104 }
105
106 if (rhs instanceof TKeypressEvent) {
107 TKeypressEvent that = (TKeypressEvent) rhs;
108 return (key.equals(that.key)
109 && (getTime().equals(that.getTime())));
110 }
111
112 TKeypress that = (TKeypress) rhs;
113 return (key.equals(that));
114 }
115
e826b451
KL
116 /**
117 * Hashcode uses all fields in equals().
118 *
119 * @return the hash
120 */
121 @Override
122 public int hashCode() {
123 int A = 13;
124 int B = 23;
125 int hash = A;
126 hash = (B * hash) + getTime().hashCode();
127 hash = (B * hash) + key.hashCode();
128 return hash;
129 }
130
df8de03f 131 /**
7b5261bc
KL
132 * Make human-readable description of this TKeypressEvent.
133 *
134 * @return displayable String
df8de03f
KL
135 */
136 @Override
b299e69c 137 public String toString() {
7b5261bc 138 return String.format("Keypress: %s", key.toString());
df8de03f
KL
139 }
140}