| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2017 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 final class TKeypressEvent extends TInputEvent { |
| 37 | |
| 38 | /** |
| 39 | * Keystroke received. |
| 40 | */ |
| 41 | private TKeypress key; |
| 42 | |
| 43 | /** |
| 44 | * Get keystroke. |
| 45 | * |
| 46 | * @return keystroke |
| 47 | */ |
| 48 | public TKeypress getKey() { |
| 49 | return key; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Public contructor. |
| 54 | * |
| 55 | * @param key the TKeypress received |
| 56 | */ |
| 57 | public TKeypressEvent(final TKeypress key) { |
| 58 | this.key = key; |
| 59 | } |
| 60 | |
| 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 | |
| 88 | this.key = new TKeypress(key.isFnKey(), key.getKeyCode(), key.getChar(), |
| 89 | alt, ctrl, shift); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Create a duplicate instance. |
| 94 | * |
| 95 | * @return duplicate intance |
| 96 | */ |
| 97 | public TKeypressEvent dup() { |
| 98 | TKeypressEvent keypress = new TKeypressEvent(key.dup()); |
| 99 | return keypress; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Comparison check. All fields must match to return true. |
| 104 | * |
| 105 | * @param rhs another TKeypressEvent or TKeypress instance |
| 106 | * @return true if all fields are equal |
| 107 | */ |
| 108 | @Override |
| 109 | public boolean equals(final Object rhs) { |
| 110 | if (!(rhs instanceof TKeypressEvent) |
| 111 | && !(rhs instanceof TKeypress) |
| 112 | ) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (rhs instanceof TKeypressEvent) { |
| 117 | TKeypressEvent that = (TKeypressEvent) rhs; |
| 118 | return (key.equals(that.key) |
| 119 | && (getTime().equals(that.getTime()))); |
| 120 | } |
| 121 | |
| 122 | TKeypress that = (TKeypress) rhs; |
| 123 | return (key.equals(that)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Hashcode uses all fields in equals(). |
| 128 | * |
| 129 | * @return the hash |
| 130 | */ |
| 131 | @Override |
| 132 | public int hashCode() { |
| 133 | int A = 13; |
| 134 | int B = 23; |
| 135 | int hash = A; |
| 136 | hash = (B * hash) + getTime().hashCode(); |
| 137 | hash = (B * hash) + key.hashCode(); |
| 138 | return hash; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Make human-readable description of this TKeypressEvent. |
| 143 | * |
| 144 | * @return displayable String |
| 145 | */ |
| 146 | @Override |
| 147 | public String toString() { |
| 148 | return String.format("Keypress: %s", key.toString()); |
| 149 | } |
| 150 | } |