JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
InputEvent.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 */
34
35package com.jogamp.newt.event;
36
37import com.jogamp.common.util.Bitfield;
38import com.jogamp.newt.Window;
39
40@SuppressWarnings("serial")
41public abstract class InputEvent extends NEWTEvent
42{
43 /** Interface marking class of input types */
44 public static interface InputClass {
45 }
46
47 /** Interface marking type of input devices */
48 public static interface InputType {
49 }
50
51 public static final int SHIFT_MASK = 1 << 0;
52 public static final int CTRL_MASK = 1 << 1;
53 public static final int META_MASK = 1 << 2;
54 public static final int ALT_MASK = 1 << 3;
55 public static final int ALT_GRAPH_MASK = 1 << 4;
56
57 public static final int BUTTON1_MASK = 1 << 5;
58 public static final int BUTTON2_MASK = 1 << 6;
59 public static final int BUTTON3_MASK = 1 << 7;
60 public static final int BUTTON4_MASK = 1 << 8;
61 public static final int BUTTON5_MASK = 1 << 9;
62 public static final int BUTTON6_MASK = 1 << 10;
63 public static final int BUTTON7_MASK = 1 << 11;
64 public static final int BUTTON8_MASK = 1 << 12;
65 public static final int BUTTON9_MASK = 1 << 13;
66
67 public static final int BUTTONLAST_MASK = 1 << 20; // 16 buttons
68 public static final int BUTTONALL_MASK = 0xffff << 5 ; // 16 buttons
69
70 /** Event is caused by auto-repeat. */
71 public static final int AUTOREPEAT_MASK = 1 << 29;
72
73 /** Pointer is confined, see {@link Window#confinePointer(boolean)}. */
74 public static final int CONFINED_MASK = 1 << 30;
75
76 /** Pointer is invisible, see {@link Window#setPointerVisible(boolean)}. */
77 public static final int INVISIBLE_MASK = 1 << 31;
78
79 /**
80 * Returns the corresponding button mask for the given button.
81 * <p>
82 * In case the given button lies outside
83 * of the valid range [{@link MouseEvent#BUTTON1} .. {@link MouseEvent#BUTTON_COUNT}],
84 * null is returned.
85 * </p>
86 */
87 public static final int getButtonMask(final int button) {
88 if( 0 < button && button <= MouseEvent.BUTTON_COUNT ) {
89 return 1 << ( 4 + button ) ;
90 }
91 return 0;
92 }
93
94 protected InputEvent(final short eventType, final Object source, final long when, final int modifiers) {
95 super(eventType, source, when);
96 this.modifiers=modifiers;
97 }
98
99 /** Return the modifier bits of this event, e.g. see {@link #SHIFT_MASK} .. etc. */
100 public final int getModifiers() {
101 return modifiers;
102 }
103 /** {@link #getModifiers()} contains {@link #ALT_MASK}. */
104 public final boolean isAltDown() {
105 return (modifiers&ALT_MASK)!=0;
106 }
107 /** {@link #getModifiers()} contains {@link #ALT_GRAPH_MASK}. */
108 public final boolean isAltGraphDown() {
109 return (modifiers&ALT_GRAPH_MASK)!=0;
110 }
111 /** {@link #getModifiers()} contains {@link #CTRL_MASK}. */
112 public final boolean isControlDown() {
113 return (modifiers&CTRL_MASK)!=0;
114 }
115 /** {@link #getModifiers()} contains {@link #META_MASK}. */
116 public final boolean isMetaDown() {
117 return (modifiers&META_MASK)!=0;
118 }
119 /** {@link #getModifiers()} contains {@link #SHIFT_MASK}. */
120 public final boolean isShiftDown() {
121 return (modifiers&SHIFT_MASK)!=0;
122 }
123 /** {@link #getModifiers()} contains {@link #AUTOREPEAT_MASK}. */
124 public final boolean isAutoRepeat() {
125 return (modifiers&AUTOREPEAT_MASK)!=0;
126 }
127 /** {@link #getModifiers()} contains {@link #CONFINED_MASK}. Pointer is confined, see {@link Window#confinePointer(boolean)}. */
128 public final boolean isConfined() {
129 return (modifiers&CONFINED_MASK)!=0;
130 }
131 /** {@link #getModifiers()} contains {@link #INVISIBLE_MASK}. Pointer is invisible, see {@link Window#setPointerVisible(boolean)}. */
132 public final boolean isInvisible() {
133 return (modifiers&INVISIBLE_MASK)!=0;
134 }
135
136 public final StringBuilder getModifiersString(StringBuilder sb) {
137 if(null == sb) {
138 sb = new StringBuilder();
139 }
140 sb.append("[");
141 boolean isFirst = true;
142 if(isShiftDown()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("shift"); }
143 if(isControlDown()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("ctrl"); }
144 if(isMetaDown()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("meta"); }
145 if(isAltDown()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("alt"); }
146 if(isAltGraphDown()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("altgr"); }
147 if(isAutoRepeat()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("repeat"); }
148 for(int i=1; i<=MouseEvent.BUTTON_COUNT; i++) {
149 if(isButtonDown(i)) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("button").append(i); }
150 }
151 if(isConfined()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("confined"); }
152 if(isInvisible()) { if(!isFirst) { sb.append(", "); } isFirst = false; sb.append("invisible"); }
153 sb.append("]");
154
155 return sb;
156 }
157
158 /**
159 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
160 * @return Array of pressed mouse buttons [{@link MouseEvent#BUTTON1} .. {@link MouseEvent#BUTTON6}].
161 * If none is down, the resulting array is of length 0.
162 */
163 public final short[] getButtonsDown() {
164 final int len = getButtonDownCount();
165
166 final short[] res = new short[len];
167 int j = 0;
168 for(int i=1; i<=MouseEvent.BUTTON_COUNT; i++) {
169 if( isButtonDown(i) ) { res[j++] = (short) ( ( MouseEvent.BUTTON1 - 1 ) + i ); }
170 }
171 return res;
172 }
173
174 /**
175 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
176 * @param button the button to test
177 * @return true if the given button is down
178 */
179 public final boolean isButtonDown(final int button) {
180 return ( modifiers & getButtonMask(button) ) != 0;
181 }
182
183 /**
184 * Returns the number of pressed buttons by counting the set bits:
185 * <pre>
186 * getBitCount(modifiers & BUTTONALL_MASK);
187 * </pre>
188 * <p>
189 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
190 * </p>
191 * @see Bitfield.Util#bitCount(int)
192 * @see #BUTTONALL_MASK
193 */
194 public final int getButtonDownCount() {
195 return Bitfield.Util.bitCount(modifiers & BUTTONALL_MASK);
196 }
197
198 /**
199 * Returns true if at least one button is pressed, otherwise false:
200 * <pre>
201 * 0 != ( modifiers & BUTTONALL_MASK )
202 * </pre>
203 * <p>
204 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
205 * </p>
206 * @see Bitfield.Util#bitCount(int)
207 * @see #BUTTONALL_MASK
208 */
209 public final boolean isAnyButtonDown() {
210 return 0 != ( modifiers & BUTTONALL_MASK );
211 }
212
213 @Override
214 public String toString() {
215 return toString(null).toString();
216 }
217
218 @Override
219 public StringBuilder toString(StringBuilder sb) {
220 if(null == sb) {
221 sb = new StringBuilder();
222 }
223 sb.append("InputEvent[modifiers: ");
224 getModifiersString(sb);
225 sb.append(", ");
226 super.toString(sb).append("]");
227 return sb;
228 }
229
230 private final int modifiers;
231}
static final int getButtonMask(final int button)
Returns the corresponding button mask for the given button.
Definition: InputEvent.java:87
final boolean isMetaDown()
getModifiers() contains META_MASK.
InputEvent(final short eventType, final Object source, final long when, final int modifiers)
Definition: InputEvent.java:94
final boolean isButtonDown(final int button)
See also MouseEvent's section about Multiple-Pointer Events.
final boolean isAnyButtonDown()
Returns true if at least one button is pressed, otherwise false:
final boolean isShiftDown()
getModifiers() contains SHIFT_MASK.
final int getButtonDownCount()
Returns the number of pressed buttons by counting the set bits:
final boolean isAutoRepeat()
getModifiers() contains AUTOREPEAT_MASK.
final boolean isAltGraphDown()
getModifiers() contains ALT_GRAPH_MASK.
final int getModifiers()
Return the modifier bits of this event, e.g.
final short[] getButtonsDown()
See also MouseEvent's section about Multiple-Pointer Events.
final boolean isControlDown()
getModifiers() contains CTRL_MASK.
final boolean isConfined()
getModifiers() contains CONFINED_MASK.
final StringBuilder getModifiersString(StringBuilder sb)
final boolean isInvisible()
getModifiers() contains INVISIBLE_MASK.
StringBuilder toString(StringBuilder sb)
final boolean isAltDown()
getModifiers() contains ALT_MASK.
Pointer event of type PointerType.
Definition: MouseEvent.java:74
static final short BUTTON1
ID for button 1, value 1
static final short BUTTON_COUNT
Maximum number of buttons, value 16
NEWT events are provided for notification purposes ONLY; The NEWT will automatically handle the even...
Definition: NEWTEvent.java:52
Interface marking class of input types.
Definition: InputEvent.java:44
Interface marking type of input devices.
Definition: InputEvent.java:48