author | dav |
Fri, 22 May 2009 16:09:45 +0400 | |
changeset 2810 | fa49c6a06baf |
parent 1962 | 6c293d33645b |
child 3938 | ef327bd847c0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.awt.event; |
|
27 |
||
28 |
import java.awt.Event; |
|
29 |
import java.awt.Component; |
|
30 |
import java.awt.GraphicsEnvironment; |
|
31 |
import java.awt.Toolkit; |
|
32 |
import java.util.logging.Logger; |
|
33 |
import java.util.logging.Level; |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
34 |
import java.util.Arrays; |
2 | 35 |
|
36 |
/** |
|
37 |
* The root event class for all component-level input events. |
|
38 |
* |
|
39 |
* Input events are delivered to listeners before they are |
|
40 |
* processed normally by the source where they originated. |
|
41 |
* This allows listeners and component subclasses to "consume" |
|
42 |
* the event so that the source will not process them in their |
|
43 |
* default manner. For example, consuming mousePressed events |
|
44 |
* on a Button component will prevent the Button from being |
|
45 |
* activated. |
|
46 |
* |
|
47 |
* @author Carl Quinn |
|
48 |
* |
|
49 |
* @see KeyEvent |
|
50 |
* @see KeyAdapter |
|
51 |
* @see MouseEvent |
|
52 |
* @see MouseAdapter |
|
53 |
* @see MouseMotionAdapter |
|
54 |
* |
|
55 |
* @since 1.1 |
|
56 |
*/ |
|
57 |
public abstract class InputEvent extends ComponentEvent { |
|
58 |
private static final Logger log = Logger.getLogger("java.awt.event.InputEvent"); |
|
59 |
||
60 |
/** |
|
61 |
* The Shift key modifier constant. |
|
62 |
* It is recommended that SHIFT_DOWN_MASK be used instead. |
|
63 |
*/ |
|
64 |
public static final int SHIFT_MASK = Event.SHIFT_MASK; |
|
65 |
||
66 |
/** |
|
67 |
* The Control key modifier constant. |
|
68 |
* It is recommended that CTRL_DOWN_MASK be used instead. |
|
69 |
*/ |
|
70 |
public static final int CTRL_MASK = Event.CTRL_MASK; |
|
71 |
||
72 |
/** |
|
73 |
* The Meta key modifier constant. |
|
74 |
* It is recommended that META_DOWN_MASK be used instead. |
|
75 |
*/ |
|
76 |
public static final int META_MASK = Event.META_MASK; |
|
77 |
||
78 |
/** |
|
79 |
* The Alt key modifier constant. |
|
80 |
* It is recommended that ALT_DOWN_MASK be used instead. |
|
81 |
*/ |
|
82 |
public static final int ALT_MASK = Event.ALT_MASK; |
|
83 |
||
84 |
/** |
|
85 |
* The AltGraph key modifier constant. |
|
86 |
*/ |
|
87 |
public static final int ALT_GRAPH_MASK = 1 << 5; |
|
88 |
||
89 |
/** |
|
90 |
* The Mouse Button1 modifier constant. |
|
91 |
* It is recommended that BUTTON1_DOWN_MASK be used instead. |
|
92 |
*/ |
|
93 |
public static final int BUTTON1_MASK = 1 << 4; |
|
94 |
||
95 |
/** |
|
96 |
* The Mouse Button2 modifier constant. |
|
97 |
* It is recommended that BUTTON2_DOWN_MASK be used instead. |
|
98 |
* Note that BUTTON2_MASK has the same value as ALT_MASK. |
|
99 |
*/ |
|
100 |
public static final int BUTTON2_MASK = Event.ALT_MASK; |
|
101 |
||
102 |
/** |
|
103 |
* The Mouse Button3 modifier constant. |
|
104 |
* It is recommended that BUTTON3_DOWN_MASK be used instead. |
|
105 |
* Note that BUTTON3_MASK has the same value as META_MASK. |
|
106 |
*/ |
|
107 |
public static final int BUTTON3_MASK = Event.META_MASK; |
|
108 |
||
109 |
/** |
|
110 |
* The Shift key extended modifier constant. |
|
111 |
* @since 1.4 |
|
112 |
*/ |
|
113 |
public static final int SHIFT_DOWN_MASK = 1 << 6; |
|
114 |
||
115 |
/** |
|
116 |
* The Control key extended modifier constant. |
|
117 |
* @since 1.4 |
|
118 |
*/ |
|
119 |
public static final int CTRL_DOWN_MASK = 1 << 7; |
|
120 |
||
121 |
/** |
|
122 |
* The Meta key extended modifier constant. |
|
123 |
* @since 1.4 |
|
124 |
*/ |
|
125 |
public static final int META_DOWN_MASK = 1 << 8; |
|
126 |
||
127 |
/** |
|
128 |
* The Alt key extended modifier constant. |
|
129 |
* @since 1.4 |
|
130 |
*/ |
|
131 |
public static final int ALT_DOWN_MASK = 1 << 9; |
|
132 |
||
133 |
/** |
|
134 |
* The Mouse Button1 extended modifier constant. |
|
135 |
* @since 1.4 |
|
136 |
*/ |
|
137 |
public static final int BUTTON1_DOWN_MASK = 1 << 10; |
|
138 |
||
139 |
/** |
|
140 |
* The Mouse Button2 extended modifier constant. |
|
141 |
* @since 1.4 |
|
142 |
*/ |
|
143 |
public static final int BUTTON2_DOWN_MASK = 1 << 11; |
|
144 |
||
145 |
/** |
|
146 |
* The Mouse Button3 extended modifier constant. |
|
147 |
* @since 1.4 |
|
148 |
*/ |
|
149 |
public static final int BUTTON3_DOWN_MASK = 1 << 12; |
|
150 |
||
151 |
/** |
|
152 |
* The AltGraph key extended modifier constant. |
|
153 |
* @since 1.4 |
|
154 |
*/ |
|
155 |
public static final int ALT_GRAPH_DOWN_MASK = 1 << 13; |
|
156 |
||
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
157 |
/** |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
158 |
* An array of extended modifiers for additional buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
159 |
* @see getButtonDownMasks |
2810
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
160 |
* There are twenty buttons fit into 4byte space. |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
161 |
* one more bit is reserved for FIRST_HIGH_BIT. |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
162 |
* @since 7.0 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
163 |
*/ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
164 |
private static final int [] BUTTON_DOWN_MASK = new int [] { BUTTON1_DOWN_MASK, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
165 |
BUTTON2_DOWN_MASK, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
166 |
BUTTON3_DOWN_MASK, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
167 |
1<<14, //4th phisical button (this is not a wheel!) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
168 |
1<<15, //(this is not a wheel!) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
169 |
1<<16, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
170 |
1<<17, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
171 |
1<<18, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
172 |
1<<19, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
173 |
1<<20, |
2810
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
174 |
1<<21, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
175 |
1<<22, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
176 |
1<<23, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
177 |
1<<24, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
178 |
1<<25, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
179 |
1<<26, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
180 |
1<<27, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
181 |
1<<28, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
182 |
1<<29, |
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
183 |
1<<30}; |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
184 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
185 |
/** |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
186 |
* A method to access an array of extended modifiers for additional buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
187 |
* @since 7.0 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
188 |
*/ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
189 |
private static int [] getButtonDownMasks(){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
190 |
return Arrays.copyOf(BUTTON_DOWN_MASK, BUTTON_DOWN_MASK.length); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
191 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
192 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
193 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
194 |
/** |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
195 |
* A method to obtain a mask for any existing mouse button. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
196 |
* The returned mask may be used for different purposes. Following are some of them: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
197 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
198 |
* <li> {@link java.awt.Robot#mousePress(int) mousePress(buttons)} and |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
199 |
* {@link java.awt.Robot#mouseRelease(int) mouseRelease(buttons)} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
200 |
* <li> as a {@code modifiers} parameter when creating a new {@link MouseEvent} instance |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
201 |
* <li> to check {@link MouseEvent#getModifiersEx() modifiersEx} of existing {@code MouseEvent} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
202 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
203 |
* @param button is a number to represent a button starting from 1. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
204 |
* For example, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
205 |
* <pre> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
206 |
* int button = InputEvent.getMaskForButton(1); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
207 |
* </pre> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
208 |
* will have the same meaning as |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
209 |
* <pre> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
210 |
* int button = InputEvent.getMaskForButton(MouseEvent.BUTTON1); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
211 |
* </pre> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
212 |
* because {@link MouseEvent#BUTTON1 MouseEvent.BUTTON1} equals to 1. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
213 |
* If a mouse has three enabled buttons(see {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()}) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
214 |
* then the values from the left column passed into the method will return |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
215 |
* corresponding values from the right column: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
216 |
* <PRE> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
217 |
* <b>button </b> <b>returned mask</b> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
218 |
* {@link MouseEvent#BUTTON1 BUTTON1} {@link MouseEvent#BUTTON1_DOWN_MASK BUTTON1_DOWN_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
219 |
* {@link MouseEvent#BUTTON2 BUTTON2} {@link MouseEvent#BUTTON2_DOWN_MASK BUTTON2_DOWN_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
220 |
* {@link MouseEvent#BUTTON3 BUTTON3} {@link MouseEvent#BUTTON3_DOWN_MASK BUTTON3_DOWN_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
221 |
* </PRE> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
222 |
* If a mouse has more than three enabled buttons then more values |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
223 |
* are admissible (4, 5, etc.). There is no assigned constants for these extended buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
224 |
* The button masks for the extra buttons returned by this method have no assigned names like the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
225 |
* first three button masks. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
226 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
227 |
* This method has the following implementation restriction. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
228 |
* It returns masks for a limited number of buttons only. The maximum number is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
229 |
* implementation dependent and may vary. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
230 |
* This limit is defined by the relevant number |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
231 |
* of buttons that may hypothetically exist on the mouse but it is greater than the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
232 |
* {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
233 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
234 |
* @throws IllegalArgumentException if {@code button} is less than zero or greater than the number |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
235 |
* of button masks reserved for buttons |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
236 |
* @since 7.0 |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
237 |
* @see java.awt.MouseInfo#getNumberOfButtons() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
238 |
* @see Toolkit#areExtraMouseButtonsEnabled() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
239 |
* @see MouseEvent#getModifiers() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
240 |
* @see MouseEvent#getModifiersEx() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
241 |
*/ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
242 |
public static int getMaskForButton(int button) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
243 |
if (button <= 0 || button > BUTTON_DOWN_MASK.length) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
244 |
throw new IllegalArgumentException("button doesn\'t exist " + button); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
245 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
246 |
return BUTTON_DOWN_MASK[button - 1]; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
247 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
248 |
|
2 | 249 |
// the constant below MUST be updated if any extra modifier |
250 |
// bits are to be added! |
|
251 |
// in fact, it is undesirable to add modifier bits |
|
252 |
// to the same field as this may break applications |
|
253 |
// see bug# 5066958 |
|
2810
fa49c6a06baf
6799099: All automatic regression tests that create Robot fail on X11
dav
parents:
1962
diff
changeset
|
254 |
static final int FIRST_HIGH_BIT = 1 << 31; |
2 | 255 |
|
256 |
static final int JDK_1_3_MODIFIERS = SHIFT_DOWN_MASK - 1; |
|
257 |
static final int HIGH_MODIFIERS = ~( FIRST_HIGH_BIT - 1 ); |
|
258 |
||
259 |
/** |
|
260 |
* The input event's Time stamp in UTC format. The time stamp |
|
261 |
* indicates when the input event was created. |
|
262 |
* |
|
263 |
* @serial |
|
264 |
* @see #getWhen() |
|
265 |
*/ |
|
266 |
long when; |
|
267 |
||
268 |
/** |
|
269 |
* The state of the modifier mask at the time the input |
|
270 |
* event was fired. |
|
271 |
* |
|
272 |
* @serial |
|
273 |
* @see #getModifiers() |
|
274 |
* @see #getModifiersEx() |
|
275 |
* @see java.awt.event.KeyEvent |
|
276 |
* @see java.awt.event.MouseEvent |
|
277 |
*/ |
|
278 |
int modifiers; |
|
279 |
||
280 |
/* |
|
281 |
* A flag that indicates that this instance can be used to access |
|
282 |
* the system clipboard. |
|
283 |
*/ |
|
284 |
private transient boolean canAccessSystemClipboard; |
|
285 |
||
286 |
static { |
|
287 |
/* ensure that the necessary native libraries are loaded */ |
|
288 |
NativeLibLoader.loadLibraries(); |
|
289 |
if (!GraphicsEnvironment.isHeadless()) { |
|
290 |
initIDs(); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Initialize JNI field and method IDs for fields that may be |
|
296 |
accessed from C. |
|
297 |
*/ |
|
298 |
private static native void initIDs(); |
|
299 |
||
300 |
/** |
|
301 |
* Constructs an InputEvent object with the specified source component, |
|
302 |
* modifiers, and type. |
|
440 | 303 |
* <p> This method throws an |
2 | 304 |
* <code>IllegalArgumentException</code> if <code>source</code> |
305 |
* is <code>null</code>. |
|
306 |
* |
|
307 |
* @param source the object where the event originated |
|
440 | 308 |
* @param id the integer that identifies the event type. |
309 |
* It is allowed to pass as parameter any value that |
|
310 |
* allowed for some subclass of {@code InputEvent} class. |
|
311 |
* Passing in the value different from those values result |
|
312 |
* in unspecified behavior |
|
313 |
* @param when a long int that gives the time the event occurred. |
|
314 |
* Passing negative or zero value |
|
315 |
* is not recommended |
|
316 |
* @param modifiers the modifier keys down during event (e.g. shift, ctrl, |
|
317 |
* alt, meta) |
|
318 |
* Passing negative parameter is not recommended. |
|
319 |
* Zero value means no modifiers. |
|
320 |
* Either extended _DOWN_MASK or old _MASK modifiers |
|
321 |
* should be used, but both models should not be mixed |
|
322 |
* in one event. Use of the extended modifiers is |
|
323 |
* preferred |
|
2 | 324 |
* @throws IllegalArgumentException if <code>source</code> is null |
440 | 325 |
* @see #getSource() |
326 |
* @see #getID() |
|
327 |
* @see #getWhen() |
|
328 |
* @see #getModifiers() |
|
2 | 329 |
*/ |
330 |
InputEvent(Component source, int id, long when, int modifiers) { |
|
331 |
super(source, id); |
|
332 |
this.when = when; |
|
333 |
this.modifiers = modifiers; |
|
334 |
canAccessSystemClipboard = canAccessSystemClipboard(); |
|
335 |
} |
|
336 |
||
337 |
private boolean canAccessSystemClipboard() { |
|
338 |
boolean b = false; |
|
339 |
||
340 |
if (!GraphicsEnvironment.isHeadless()) { |
|
341 |
SecurityManager sm = System.getSecurityManager(); |
|
342 |
if (sm != null) { |
|
343 |
try { |
|
344 |
sm.checkSystemClipboardAccess(); |
|
345 |
b = true; |
|
346 |
} catch (SecurityException se) { |
|
347 |
if (log.isLoggable(Level.FINE)) { |
|
348 |
log.log(Level.FINE, "InputEvent.canAccessSystemClipboard() got SecurityException ", se); |
|
349 |
} |
|
350 |
} |
|
351 |
} else { |
|
352 |
b = true; |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
return b; |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Returns whether or not the Shift modifier is down on this event. |
|
361 |
*/ |
|
362 |
public boolean isShiftDown() { |
|
363 |
return (modifiers & SHIFT_MASK) != 0; |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Returns whether or not the Control modifier is down on this event. |
|
368 |
*/ |
|
369 |
public boolean isControlDown() { |
|
370 |
return (modifiers & CTRL_MASK) != 0; |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Returns whether or not the Meta modifier is down on this event. |
|
375 |
*/ |
|
376 |
public boolean isMetaDown() { |
|
377 |
return (modifiers & META_MASK) != 0; |
|
378 |
} |
|
379 |
||
380 |
/** |
|
381 |
* Returns whether or not the Alt modifier is down on this event. |
|
382 |
*/ |
|
383 |
public boolean isAltDown() { |
|
384 |
return (modifiers & ALT_MASK) != 0; |
|
385 |
} |
|
386 |
||
387 |
/** |
|
388 |
* Returns whether or not the AltGraph modifier is down on this event. |
|
389 |
*/ |
|
390 |
public boolean isAltGraphDown() { |
|
391 |
return (modifiers & ALT_GRAPH_MASK) != 0; |
|
392 |
} |
|
393 |
||
394 |
/** |
|
440 | 395 |
* Returns the difference in milliseconds between the timestamp of when this event occurred and |
396 |
* midnight, January 1, 1970 UTC. |
|
2 | 397 |
*/ |
398 |
public long getWhen() { |
|
399 |
return when; |
|
400 |
} |
|
401 |
||
402 |
/** |
|
403 |
* Returns the modifier mask for this event. |
|
404 |
*/ |
|
405 |
public int getModifiers() { |
|
406 |
return modifiers & (JDK_1_3_MODIFIERS | HIGH_MODIFIERS); |
|
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* Returns the extended modifier mask for this event. |
|
411 |
* Extended modifiers represent the state of all modal keys, |
|
412 |
* such as ALT, CTRL, META, and the mouse buttons just after |
|
413 |
* the event occurred |
|
414 |
* <P> |
|
415 |
* For example, if the user presses <b>button 1</b> followed by |
|
416 |
* <b>button 2</b>, and then releases them in the same order, |
|
417 |
* the following sequence of events is generated: |
|
418 |
* <PRE> |
|
419 |
* <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK</code> |
|
420 |
* <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK</code> |
|
421 |
* <code>MOUSE_RELEASED</code>: <code>BUTTON2_DOWN_MASK</code> |
|
422 |
* <code>MOUSE_CLICKED</code>: <code>BUTTON2_DOWN_MASK</code> |
|
423 |
* <code>MOUSE_RELEASED</code>: |
|
424 |
* <code>MOUSE_CLICKED</code>: |
|
425 |
* </PRE> |
|
426 |
* <P> |
|
427 |
* It is not recommended to compare the return value of this method |
|
428 |
* using <code>==</code> because new modifiers can be added in the future. |
|
429 |
* For example, the appropriate way to check that SHIFT and BUTTON1 are |
|
430 |
* down, but CTRL is up is demonstrated by the following code: |
|
431 |
* <PRE> |
|
432 |
* int onmask = SHIFT_DOWN_MASK | BUTTON1_DOWN_MASK; |
|
433 |
* int offmask = CTRL_DOWN_MASK; |
|
434 |
* if ((event.getModifiersEx() & (onmask | offmask)) == onmask) { |
|
435 |
* ... |
|
436 |
* } |
|
437 |
* </PRE> |
|
438 |
* The above code will work even if new modifiers are added. |
|
439 |
* |
|
440 |
* @since 1.4 |
|
441 |
*/ |
|
442 |
public int getModifiersEx() { |
|
443 |
return modifiers & ~JDK_1_3_MODIFIERS; |
|
444 |
} |
|
445 |
||
446 |
/** |
|
447 |
* Consumes this event so that it will not be processed |
|
448 |
* in the default manner by the source which originated it. |
|
449 |
*/ |
|
450 |
public void consume() { |
|
451 |
consumed = true; |
|
452 |
} |
|
453 |
||
454 |
/** |
|
455 |
* Returns whether or not this event has been consumed. |
|
456 |
* @see #consume |
|
457 |
*/ |
|
458 |
public boolean isConsumed() { |
|
459 |
return consumed; |
|
460 |
} |
|
461 |
||
462 |
// state serialization compatibility with JDK 1.1 |
|
463 |
static final long serialVersionUID = -2482525981698309786L; |
|
464 |
||
465 |
/** |
|
466 |
* Returns a String describing the extended modifier keys and |
|
467 |
* mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift". |
|
468 |
* These strings can be localized by changing the |
|
440 | 469 |
* <code>awt.properties</code> file. |
470 |
* <p> |
|
471 |
* Note that passing negative parameter is incorrect, |
|
472 |
* and will cause the returning an unspecified string. |
|
473 |
* Zero parameter means that no modifiers were passed and will |
|
474 |
* cause the returning an empty string. |
|
2 | 475 |
* |
476 |
* @param modifiers a modifier mask describing the extended |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
477 |
* modifier keys and mouse buttons for the event |
2 | 478 |
* @return a text description of the combination of extended |
479 |
* modifier keys and mouse buttons that were held down |
|
480 |
* during the event. |
|
481 |
* @since 1.4 |
|
482 |
*/ |
|
483 |
public static String getModifiersExText(int modifiers) { |
|
484 |
StringBuilder buf = new StringBuilder(); |
|
485 |
if ((modifiers & InputEvent.META_DOWN_MASK) != 0) { |
|
486 |
buf.append(Toolkit.getProperty("AWT.meta", "Meta")); |
|
487 |
buf.append("+"); |
|
488 |
} |
|
489 |
if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) { |
|
490 |
buf.append(Toolkit.getProperty("AWT.control", "Ctrl")); |
|
491 |
buf.append("+"); |
|
492 |
} |
|
493 |
if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) { |
|
494 |
buf.append(Toolkit.getProperty("AWT.alt", "Alt")); |
|
495 |
buf.append("+"); |
|
496 |
} |
|
497 |
if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) { |
|
498 |
buf.append(Toolkit.getProperty("AWT.shift", "Shift")); |
|
499 |
buf.append("+"); |
|
500 |
} |
|
501 |
if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) { |
|
502 |
buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")); |
|
503 |
buf.append("+"); |
|
504 |
} |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
505 |
|
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
506 |
int buttonNumber = 1; |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
507 |
for (int mask : InputEvent.BUTTON_DOWN_MASK){ |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
508 |
if ((modifiers & mask) != 0) { |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
509 |
buf.append(Toolkit.getProperty("AWT.button"+buttonNumber, "Button"+buttonNumber)); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
510 |
buf.append("+"); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
511 |
} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
715
diff
changeset
|
512 |
buttonNumber++; |
2 | 513 |
} |
514 |
if (buf.length() > 0) { |
|
515 |
buf.setLength(buf.length()-1); // remove trailing '+' |
|
516 |
} |
|
517 |
return buf.toString(); |
|
518 |
} |
|
519 |
} |