1 /*
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
45 * default manner. For example, consuming mousePressed events
46 * on a Button component will prevent the Button from being
47 * activated.
48 *
49 * @author Carl Quinn
50 *
51 * @see KeyEvent
52 * @see KeyAdapter
53 * @see MouseEvent
54 * @see MouseAdapter
55 * @see MouseMotionAdapter
56 *
57 * @since 1.1
58 */
59 public abstract class InputEvent extends ComponentEvent {
60
61 private static final PlatformLogger logger = PlatformLogger.getLogger("java.awt.event.InputEvent");
62
63 /**
64 * The Shift key modifier constant.
65 * It is recommended that SHIFT_DOWN_MASK be used instead.
66 */
67 public static final int SHIFT_MASK = Event.SHIFT_MASK;
68
69 /**
70 * The Control key modifier constant.
71 * It is recommended that CTRL_DOWN_MASK be used instead.
72 */
73 public static final int CTRL_MASK = Event.CTRL_MASK;
74
75 /**
76 * The Meta key modifier constant.
77 * It is recommended that META_DOWN_MASK be used instead.
78 */
79 public static final int META_MASK = Event.META_MASK;
80
81 /**
82 * The Alt key modifier constant.
83 * It is recommended that ALT_DOWN_MASK be used instead.
84 */
85 public static final int ALT_MASK = Event.ALT_MASK;
86
87 /**
88 * The AltGraph key modifier constant.
89 */
90 public static final int ALT_GRAPH_MASK = 1 << 5;
91
92 /**
93 * The Mouse Button1 modifier constant.
94 * It is recommended that BUTTON1_DOWN_MASK be used instead.
95 */
96 public static final int BUTTON1_MASK = 1 << 4;
97
98 /**
99 * The Mouse Button2 modifier constant.
100 * It is recommended that BUTTON2_DOWN_MASK be used instead.
101 * Note that BUTTON2_MASK has the same value as ALT_MASK.
102 */
103 public static final int BUTTON2_MASK = Event.ALT_MASK;
104
105 /**
106 * The Mouse Button3 modifier constant.
107 * It is recommended that BUTTON3_DOWN_MASK be used instead.
108 * Note that BUTTON3_MASK has the same value as META_MASK.
109 */
110 public static final int BUTTON3_MASK = Event.META_MASK;
111
112 /**
113 * The Shift key extended modifier constant.
114 * @since 1.4
115 */
116 public static final int SHIFT_DOWN_MASK = 1 << 6;
117
118 /**
119 * The Control key extended modifier constant.
120 * @since 1.4
121 */
122 public static final int CTRL_DOWN_MASK = 1 << 7;
123
124 /**
125 * The Meta key extended modifier constant.
126 * @since 1.4
127 */
128 public static final int META_DOWN_MASK = 1 << 8;
129
142 /**
143 * The Mouse Button2 extended modifier constant.
144 * @since 1.4
145 */
146 public static final int BUTTON2_DOWN_MASK = 1 << 11;
147
148 /**
149 * The Mouse Button3 extended modifier constant.
150 * @since 1.4
151 */
152 public static final int BUTTON3_DOWN_MASK = 1 << 12;
153
154 /**
155 * The AltGraph key extended modifier constant.
156 * @since 1.4
157 */
158 public static final int ALT_GRAPH_DOWN_MASK = 1 << 13;
159
160 /**
161 * An array of extended modifiers for additional buttons.
162 * @see getButtonDownMasks
163 * There are twenty buttons fit into 4byte space.
164 * one more bit is reserved for FIRST_HIGH_BIT.
165 * @since 1.7
166 */
167 private static final int [] BUTTON_DOWN_MASK = new int [] { BUTTON1_DOWN_MASK,
168 BUTTON2_DOWN_MASK,
169 BUTTON3_DOWN_MASK,
170 1<<14, //4th physical button (this is not a wheel!)
171 1<<15, //(this is not a wheel!)
172 1<<16,
173 1<<17,
174 1<<18,
175 1<<19,
176 1<<20,
177 1<<21,
178 1<<22,
179 1<<23,
180 1<<24,
181 1<<25,
182 1<<26,
365 sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
366 b = true;
367 } catch (SecurityException se) {
368 if (logger.isLoggable(PlatformLogger.Level.FINE)) {
369 logger.fine("InputEvent.canAccessSystemClipboard() got SecurityException ", se);
370 }
371 }
372 } else {
373 b = true;
374 }
375 }
376
377 return b;
378 }
379
380 /**
381 * Returns whether or not the Shift modifier is down on this event.
382 * @return whether or not the Shift modifier is down on this event
383 */
384 public boolean isShiftDown() {
385 return (modifiers & SHIFT_MASK) != 0;
386 }
387
388 /**
389 * Returns whether or not the Control modifier is down on this event.
390 * @return whether or not the Control modifier is down on this event
391 */
392 public boolean isControlDown() {
393 return (modifiers & CTRL_MASK) != 0;
394 }
395
396 /**
397 * Returns whether or not the Meta modifier is down on this event.
398 * @return whether or not the Meta modifier is down on this event
399 */
400 public boolean isMetaDown() {
401 return (modifiers & META_MASK) != 0;
402 }
403
404 /**
405 * Returns whether or not the Alt modifier is down on this event.
406 * @return whether or not the Alt modifier is down on this event
407 */
408 public boolean isAltDown() {
409 return (modifiers & ALT_MASK) != 0;
410 }
411
412 /**
413 * Returns whether or not the AltGraph modifier is down on this event.
414 * @return whether or not the AltGraph modifier is down on this event
415 */
416 public boolean isAltGraphDown() {
417 return (modifiers & ALT_GRAPH_MASK) != 0;
418 }
419
420 /**
421 * Returns the difference in milliseconds between the timestamp of when this event occurred and
422 * midnight, January 1, 1970 UTC.
423 * @return the difference in milliseconds between the timestamp and midnight, January 1, 1970 UTC
424 */
425 public long getWhen() {
426 return when;
427 }
428
429 /**
430 * Returns the modifier mask for this event.
431 * @return the modifier mask for this event
432 */
433 public int getModifiers() {
434 return modifiers & (JDK_1_3_MODIFIERS | HIGH_MODIFIERS);
435 }
436
437 /**
438 * Returns the extended modifier mask for this event.
439 * <P>
440 * Extended modifiers are the modifiers that ends with the _DOWN_MASK suffix,
441 * such as ALT_DOWN_MASK, BUTTON1_DOWN_MASK, and others.
442 * <P>
443 * Extended modifiers represent the state of all modal keys,
444 * such as ALT, CTRL, META, and the mouse buttons just after
445 * the event occurred.
446 * <P>
447 * For example, if the user presses <b>button 1</b> followed by
448 * <b>button 2</b>, and then releases them in the same order,
449 * the following sequence of events is generated:
450 * <PRE>
451 * {@code MOUSE_PRESSED}: {@code BUTTON1_DOWN_MASK}
452 * {@code MOUSE_PRESSED}: {@code BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK}
|
1 /*
2 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
45 * default manner. For example, consuming mousePressed events
46 * on a Button component will prevent the Button from being
47 * activated.
48 *
49 * @author Carl Quinn
50 *
51 * @see KeyEvent
52 * @see KeyAdapter
53 * @see MouseEvent
54 * @see MouseAdapter
55 * @see MouseMotionAdapter
56 *
57 * @since 1.1
58 */
59 public abstract class InputEvent extends ComponentEvent {
60
61 private static final PlatformLogger logger = PlatformLogger.getLogger("java.awt.event.InputEvent");
62
63 /**
64 * The Shift key modifier constant.
65 *
66 * @deprecated It is recommended that SHIFT_DOWN_MASK and
67 * {@link #getModifiersEx()} be used instead
68 */
69 @Deprecated(since = "9")
70 public static final int SHIFT_MASK = Event.SHIFT_MASK;
71
72 /**
73 * The Control key modifier constant.
74 *
75 * @deprecated It is recommended that CTRL_DOWN_MASK and
76 * {@link #getModifiersEx()} be used instead
77 */
78 @Deprecated(since = "9")
79 public static final int CTRL_MASK = Event.CTRL_MASK;
80
81 /**
82 * The Meta key modifier constant.
83 *
84 * @deprecated It is recommended that META_DOWN_MASK and
85 * {@link #getModifiersEx()} be used instead
86 */
87 @Deprecated(since = "9")
88 public static final int META_MASK = Event.META_MASK;
89
90 /**
91 * The Alt key modifier constant.
92 *
93 * @deprecated It is recommended that ALT_DOWN_MASK and
94 * {@link #getModifiersEx()} be used instead
95 */
96 @Deprecated(since = "9")
97 public static final int ALT_MASK = Event.ALT_MASK;
98
99 /**
100 * The AltGraph key modifier constant.
101 *
102 * @deprecated It is recommended that ALT_GRAPH_DOWN_MASK and
103 * {@link #getModifiersEx()} be used instead
104 */
105 @Deprecated(since = "9")
106 public static final int ALT_GRAPH_MASK = 1 << 5;
107
108 /**
109 * The Mouse Button1 modifier constant.
110 *
111 * @deprecated It is recommended that BUTTON1_DOWN_MASK and
112 * {@link #getModifiersEx()} be used instead
113 */
114 @Deprecated(since = "9")
115 public static final int BUTTON1_MASK = 1 << 4;
116
117 /**
118 * The Mouse Button2 modifier constant.
119 *
120 * @deprecated It is recommended that BUTTON2_DOWN_MASK and
121 * {@link #getModifiersEx()} be used instead. Note that
122 * BUTTON2_MASK has the same value as ALT_MASK.
123 */
124 @Deprecated(since = "9")
125 public static final int BUTTON2_MASK = Event.ALT_MASK;
126
127 /**
128 * The Mouse Button3 modifier constant.
129 *
130 * @deprecated It is recommended that BUTTON3_DOWN_MASK and
131 * {@link #getModifiersEx()} be used instead. Note that
132 * BUTTON3_MASK has the same value as META_MASK.
133 */
134 @Deprecated(since = "9")
135 public static final int BUTTON3_MASK = Event.META_MASK;
136
137 /**
138 * The Shift key extended modifier constant.
139 * @since 1.4
140 */
141 public static final int SHIFT_DOWN_MASK = 1 << 6;
142
143 /**
144 * The Control key extended modifier constant.
145 * @since 1.4
146 */
147 public static final int CTRL_DOWN_MASK = 1 << 7;
148
149 /**
150 * The Meta key extended modifier constant.
151 * @since 1.4
152 */
153 public static final int META_DOWN_MASK = 1 << 8;
154
167 /**
168 * The Mouse Button2 extended modifier constant.
169 * @since 1.4
170 */
171 public static final int BUTTON2_DOWN_MASK = 1 << 11;
172
173 /**
174 * The Mouse Button3 extended modifier constant.
175 * @since 1.4
176 */
177 public static final int BUTTON3_DOWN_MASK = 1 << 12;
178
179 /**
180 * The AltGraph key extended modifier constant.
181 * @since 1.4
182 */
183 public static final int ALT_GRAPH_DOWN_MASK = 1 << 13;
184
185 /**
186 * An array of extended modifiers for additional buttons.
187 * @see #getButtonDownMasks()
188 * There are twenty buttons fit into 4byte space.
189 * one more bit is reserved for FIRST_HIGH_BIT.
190 * @since 1.7
191 */
192 private static final int [] BUTTON_DOWN_MASK = new int [] { BUTTON1_DOWN_MASK,
193 BUTTON2_DOWN_MASK,
194 BUTTON3_DOWN_MASK,
195 1<<14, //4th physical button (this is not a wheel!)
196 1<<15, //(this is not a wheel!)
197 1<<16,
198 1<<17,
199 1<<18,
200 1<<19,
201 1<<20,
202 1<<21,
203 1<<22,
204 1<<23,
205 1<<24,
206 1<<25,
207 1<<26,
390 sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
391 b = true;
392 } catch (SecurityException se) {
393 if (logger.isLoggable(PlatformLogger.Level.FINE)) {
394 logger.fine("InputEvent.canAccessSystemClipboard() got SecurityException ", se);
395 }
396 }
397 } else {
398 b = true;
399 }
400 }
401
402 return b;
403 }
404
405 /**
406 * Returns whether or not the Shift modifier is down on this event.
407 * @return whether or not the Shift modifier is down on this event
408 */
409 public boolean isShiftDown() {
410 return (modifiers & SHIFT_DOWN_MASK) != 0;
411 }
412
413 /**
414 * Returns whether or not the Control modifier is down on this event.
415 * @return whether or not the Control modifier is down on this event
416 */
417 public boolean isControlDown() {
418 return (modifiers & CTRL_DOWN_MASK) != 0;
419 }
420
421 /**
422 * Returns whether or not the Meta modifier is down on this event.
423 * @return whether or not the Meta modifier is down on this event
424 */
425 public boolean isMetaDown() {
426 return (modifiers & META_DOWN_MASK) != 0;
427 }
428
429 /**
430 * Returns whether or not the Alt modifier is down on this event.
431 * @return whether or not the Alt modifier is down on this event
432 */
433 public boolean isAltDown() {
434 return (modifiers & ALT_DOWN_MASK) != 0;
435 }
436
437 /**
438 * Returns whether or not the AltGraph modifier is down on this event.
439 * @return whether or not the AltGraph modifier is down on this event
440 */
441 public boolean isAltGraphDown() {
442 return (modifiers & ALT_GRAPH_DOWN_MASK) != 0;
443 }
444
445 /**
446 * Returns the difference in milliseconds between the timestamp of when this event occurred and
447 * midnight, January 1, 1970 UTC.
448 * @return the difference in milliseconds between the timestamp and midnight, January 1, 1970 UTC
449 */
450 public long getWhen() {
451 return when;
452 }
453
454 /**
455 * Returns the modifier mask for this event.
456 *
457 * @return the modifier mask for this event
458 * @deprecated It is recommended that extended modifier keys and
459 * {@link #getModifiersEx()} be used instead
460 */
461 @Deprecated(since = "9")
462 public int getModifiers() {
463 return modifiers & (JDK_1_3_MODIFIERS | HIGH_MODIFIERS);
464 }
465
466 /**
467 * Returns the extended modifier mask for this event.
468 * <P>
469 * Extended modifiers are the modifiers that ends with the _DOWN_MASK suffix,
470 * such as ALT_DOWN_MASK, BUTTON1_DOWN_MASK, and others.
471 * <P>
472 * Extended modifiers represent the state of all modal keys,
473 * such as ALT, CTRL, META, and the mouse buttons just after
474 * the event occurred.
475 * <P>
476 * For example, if the user presses <b>button 1</b> followed by
477 * <b>button 2</b>, and then releases them in the same order,
478 * the following sequence of events is generated:
479 * <PRE>
480 * {@code MOUSE_PRESSED}: {@code BUTTON1_DOWN_MASK}
481 * {@code MOUSE_PRESSED}: {@code BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK}
|