• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 /**
18  * @author Michael Danilov
19  * @version $Revision$
20  */
21 package java.awt.event;
22 
23 import java.awt.Component;
24 import java.awt.Toolkit;
25 import java.lang.reflect.Field;
26 import java.lang.reflect.Modifier;
27 
28 import org.apache.harmony.awt.internal.nls.Messages;
29 
30 /**
31  * This class is not supported in Android 1.0. It is merely provided to maintain
32  * interface compatibility with desktop Java implementations.
33  *
34  * @since Android 1.0
35  */
36 public class KeyEvent extends InputEvent {
37 
38     private static final long serialVersionUID = -2352130953028126954L;
39 
40     public static final int KEY_FIRST = 400;
41 
42     public static final int KEY_LAST = 402;
43 
44     public static final int KEY_TYPED = 400;
45 
46     public static final int KEY_PRESSED = 401;
47 
48     public static final int KEY_RELEASED = 402;
49 
50     public static final int VK_ENTER = 10;
51 
52     public static final int VK_BACK_SPACE = 8;
53 
54     public static final int VK_TAB = 9;
55 
56     public static final int VK_CANCEL = 3;
57 
58     public static final int VK_CLEAR = 12;
59 
60     public static final int VK_SHIFT = 16;
61 
62     public static final int VK_CONTROL = 17;
63 
64     public static final int VK_ALT = 18;
65 
66     public static final int VK_PAUSE = 19;
67 
68     public static final int VK_CAPS_LOCK = 20;
69 
70     public static final int VK_ESCAPE = 27;
71 
72     public static final int VK_SPACE = 32;
73 
74     public static final int VK_PAGE_UP = 33;
75 
76     public static final int VK_PAGE_DOWN = 34;
77 
78     public static final int VK_END = 35;
79 
80     public static final int VK_HOME = 36;
81 
82     public static final int VK_LEFT = 37;
83 
84     public static final int VK_UP = 38;
85 
86     public static final int VK_RIGHT = 39;
87 
88     public static final int VK_DOWN = 40;
89 
90     public static final int VK_COMMA = 44;
91 
92     public static final int VK_MINUS = 45;
93 
94     public static final int VK_PERIOD = 46;
95 
96     public static final int VK_SLASH = 47;
97 
98     public static final int VK_0 = 48;
99 
100     public static final int VK_1 = 49;
101 
102     public static final int VK_2 = 50;
103 
104     public static final int VK_3 = 51;
105 
106     public static final int VK_4 = 52;
107 
108     public static final int VK_5 = 53;
109 
110     public static final int VK_6 = 54;
111 
112     public static final int VK_7 = 55;
113 
114     public static final int VK_8 = 56;
115 
116     public static final int VK_9 = 57;
117 
118     public static final int VK_SEMICOLON = 59;
119 
120     public static final int VK_EQUALS = 61;
121 
122     public static final int VK_A = 65;
123 
124     public static final int VK_B = 66;
125 
126     public static final int VK_C = 67;
127 
128     public static final int VK_D = 68;
129 
130     public static final int VK_E = 69;
131 
132     public static final int VK_F = 70;
133 
134     public static final int VK_G = 71;
135 
136     public static final int VK_H = 72;
137 
138     public static final int VK_I = 73;
139 
140     public static final int VK_J = 74;
141 
142     public static final int VK_K = 75;
143 
144     public static final int VK_L = 76;
145 
146     public static final int VK_M = 77;
147 
148     public static final int VK_N = 78;
149 
150     public static final int VK_O = 79;
151 
152     public static final int VK_P = 80;
153 
154     public static final int VK_Q = 81;
155 
156     public static final int VK_R = 82;
157 
158     public static final int VK_S = 83;
159 
160     public static final int VK_T = 84;
161 
162     public static final int VK_U = 85;
163 
164     public static final int VK_V = 86;
165 
166     public static final int VK_W = 87;
167 
168     public static final int VK_X = 88;
169 
170     public static final int VK_Y = 89;
171 
172     public static final int VK_Z = 90;
173 
174     public static final int VK_OPEN_BRACKET = 91;
175 
176     public static final int VK_BACK_SLASH = 92;
177 
178     public static final int VK_CLOSE_BRACKET = 93;
179 
180     public static final int VK_NUMPAD0 = 96;
181 
182     public static final int VK_NUMPAD1 = 97;
183 
184     public static final int VK_NUMPAD2 = 98;
185 
186     public static final int VK_NUMPAD3 = 99;
187 
188     public static final int VK_NUMPAD4 = 100;
189 
190     public static final int VK_NUMPAD5 = 101;
191 
192     public static final int VK_NUMPAD6 = 102;
193 
194     public static final int VK_NUMPAD7 = 103;
195 
196     public static final int VK_NUMPAD8 = 104;
197 
198     public static final int VK_NUMPAD9 = 105;
199 
200     public static final int VK_MULTIPLY = 106;
201 
202     public static final int VK_ADD = 107;
203 
204     public static final int VK_SEPARATER = 108;
205 
206     public static final int VK_SEPARATOR = 108;
207 
208     public static final int VK_SUBTRACT = 109;
209 
210     public static final int VK_DECIMAL = 110;
211 
212     public static final int VK_DIVIDE = 111;
213 
214     public static final int VK_DELETE = 127;
215 
216     public static final int VK_NUM_LOCK = 144;
217 
218     public static final int VK_SCROLL_LOCK = 145;
219 
220     public static final int VK_F1 = 112;
221 
222     public static final int VK_F2 = 113;
223 
224     public static final int VK_F3 = 114;
225 
226     public static final int VK_F4 = 115;
227 
228     public static final int VK_F5 = 116;
229 
230     public static final int VK_F6 = 117;
231 
232     public static final int VK_F7 = 118;
233 
234     public static final int VK_F8 = 119;
235 
236     public static final int VK_F9 = 120;
237 
238     public static final int VK_F10 = 121;
239 
240     public static final int VK_F11 = 122;
241 
242     public static final int VK_F12 = 123;
243 
244     public static final int VK_F13 = 61440;
245 
246     public static final int VK_F14 = 61441;
247 
248     public static final int VK_F15 = 61442;
249 
250     public static final int VK_F16 = 61443;
251 
252     public static final int VK_F17 = 61444;
253 
254     public static final int VK_F18 = 61445;
255 
256     public static final int VK_F19 = 61446;
257 
258     public static final int VK_F20 = 61447;
259 
260     public static final int VK_F21 = 61448;
261 
262     public static final int VK_F22 = 61449;
263 
264     public static final int VK_F23 = 61450;
265 
266     public static final int VK_F24 = 61451;
267 
268     public static final int VK_PRINTSCREEN = 154;
269 
270     public static final int VK_INSERT = 155;
271 
272     public static final int VK_HELP = 156;
273 
274     public static final int VK_META = 157;
275 
276     public static final int VK_BACK_QUOTE = 192;
277 
278     public static final int VK_QUOTE = 222;
279 
280     public static final int VK_KP_UP = 224;
281 
282     public static final int VK_KP_DOWN = 225;
283 
284     public static final int VK_KP_LEFT = 226;
285 
286     public static final int VK_KP_RIGHT = 227;
287 
288     public static final int VK_DEAD_GRAVE = 128;
289 
290     public static final int VK_DEAD_ACUTE = 129;
291 
292     public static final int VK_DEAD_CIRCUMFLEX = 130;
293 
294     public static final int VK_DEAD_TILDE = 131;
295 
296     public static final int VK_DEAD_MACRON = 132;
297 
298     public static final int VK_DEAD_BREVE = 133;
299 
300     public static final int VK_DEAD_ABOVEDOT = 134;
301 
302     public static final int VK_DEAD_DIAERESIS = 135;
303 
304     public static final int VK_DEAD_ABOVERING = 136;
305 
306     public static final int VK_DEAD_DOUBLEACUTE = 137;
307 
308     public static final int VK_DEAD_CARON = 138;
309 
310     public static final int VK_DEAD_CEDILLA = 139;
311 
312     public static final int VK_DEAD_OGONEK = 140;
313 
314     public static final int VK_DEAD_IOTA = 141;
315 
316     public static final int VK_DEAD_VOICED_SOUND = 142;
317 
318     public static final int VK_DEAD_SEMIVOICED_SOUND = 143;
319 
320     public static final int VK_AMPERSAND = 150;
321 
322     public static final int VK_ASTERISK = 151;
323 
324     public static final int VK_QUOTEDBL = 152;
325 
326     public static final int VK_LESS = 153;
327 
328     public static final int VK_GREATER = 160;
329 
330     public static final int VK_BRACELEFT = 161;
331 
332     public static final int VK_BRACERIGHT = 162;
333 
334     public static final int VK_AT = 512;
335 
336     public static final int VK_COLON = 513;
337 
338     public static final int VK_CIRCUMFLEX = 514;
339 
340     public static final int VK_DOLLAR = 515;
341 
342     public static final int VK_EURO_SIGN = 516;
343 
344     public static final int VK_EXCLAMATION_MARK = 517;
345 
346     public static final int VK_INVERTED_EXCLAMATION_MARK = 518;
347 
348     public static final int VK_LEFT_PARENTHESIS = 519;
349 
350     public static final int VK_NUMBER_SIGN = 520;
351 
352     public static final int VK_PLUS = 521;
353 
354     public static final int VK_RIGHT_PARENTHESIS = 522;
355 
356     public static final int VK_UNDERSCORE = 523;
357 
358     public static final int VK_FINAL = 24;
359 
360     public static final int VK_WINDOWS = 524;
361 
362     public static final int VK_CONTEXT_MENU = 525;
363 
364     public static final int VK_CONVERT = 28;
365 
366     public static final int VK_NONCONVERT = 29;
367 
368     public static final int VK_ACCEPT = 30;
369 
370     public static final int VK_MODECHANGE = 31;
371 
372     public static final int VK_KANA = 21;
373 
374     public static final int VK_KANJI = 25;
375 
376     public static final int VK_ALPHANUMERIC = 240;
377 
378     public static final int VK_KATAKANA = 241;
379 
380     public static final int VK_HIRAGANA = 242;
381 
382     public static final int VK_FULL_WIDTH = 243;
383 
384     public static final int VK_HALF_WIDTH = 244;
385 
386     public static final int VK_ROMAN_CHARACTERS = 245;
387 
388     public static final int VK_ALL_CANDIDATES = 256;
389 
390     public static final int VK_PREVIOUS_CANDIDATE = 257;
391 
392     public static final int VK_CODE_INPUT = 258;
393 
394     public static final int VK_JAPANESE_KATAKANA = 259;
395 
396     public static final int VK_JAPANESE_HIRAGANA = 260;
397 
398     public static final int VK_JAPANESE_ROMAN = 261;
399 
400     public static final int VK_KANA_LOCK = 262;
401 
402     public static final int VK_INPUT_METHOD_ON_OFF = 263;
403 
404     public static final int VK_CUT = 65489;
405 
406     public static final int VK_COPY = 65485;
407 
408     public static final int VK_PASTE = 65487;
409 
410     public static final int VK_UNDO = 65483;
411 
412     public static final int VK_AGAIN = 65481;
413 
414     public static final int VK_FIND = 65488;
415 
416     public static final int VK_PROPS = 65482;
417 
418     public static final int VK_STOP = 65480;
419 
420     public static final int VK_COMPOSE = 65312;
421 
422     public static final int VK_ALT_GRAPH = 65406;
423 
424     public static final int VK_BEGIN = 65368;
425 
426     public static final int VK_UNDEFINED = 0;
427 
428     public static final char CHAR_UNDEFINED = (char)(-1);
429 
430     public static final int KEY_LOCATION_UNKNOWN = 0;
431 
432     public static final int KEY_LOCATION_STANDARD = 1;
433 
434     public static final int KEY_LOCATION_LEFT = 2;
435 
436     public static final int KEY_LOCATION_RIGHT = 3;
437 
438     public static final int KEY_LOCATION_NUMPAD = 4;
439 
440     private int keyCode;
441     private char keyChar;
442     private int keyLocation;
443 
getKeyModifiersText(int modifiers)444     public static String getKeyModifiersText(int modifiers) {
445         return getKeyModifiersExText(extractExFlags(modifiers));
446     }
447 
getKeyModifiersExText(int modifiersEx)448     static String getKeyModifiersExText(int modifiersEx) {
449         String text = ""; //$NON-NLS-1$
450 
451         if ((modifiersEx & InputEvent.META_DOWN_MASK) != 0) {
452             text += Toolkit.getProperty("AWT.meta", "Meta"); //$NON-NLS-1$ //$NON-NLS-2$
453         }
454         if ((modifiersEx & InputEvent.CTRL_DOWN_MASK) != 0) {
455             text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
456                     Toolkit.getProperty("AWT.control", "Ctrl"); //$NON-NLS-1$ //$NON-NLS-2$
457         }
458         if ((modifiersEx & InputEvent.ALT_DOWN_MASK) != 0) {
459             text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
460                     Toolkit.getProperty("AWT.alt", "Alt"); //$NON-NLS-1$ //$NON-NLS-2$
461         }
462         if ((modifiersEx & InputEvent.SHIFT_DOWN_MASK) != 0) {
463             text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
464                     Toolkit.getProperty("AWT.shift", "Shift"); //$NON-NLS-1$ //$NON-NLS-2$
465         }
466         if ((modifiersEx & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
467             text += ((text.length() > 0) ? "+" : "") + //$NON-NLS-1$ //$NON-NLS-2$
468                     Toolkit.getProperty("AWT.altGraph", "Alt Graph"); //$NON-NLS-1$ //$NON-NLS-2$
469         }
470 
471         return text;
472     }
473 
getKeyText(int keyCode)474     public static String getKeyText(int keyCode) {
475         String[] rawName = getPublicStaticFinalIntFieldName(keyCode); //$NON-NLS-1$
476 
477         if ((rawName == null) || (rawName.length == 0)) {
478             return ("Unknown keyCode: " + (keyCode >= 0 ? "0x" : "-0x") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
479                     Integer.toHexString(Math.abs(keyCode)));
480         }
481 
482         String propertyName = getPropertyName(rawName);
483         String defaultName = getDefaultName(rawName);
484 
485         return Toolkit.getProperty(propertyName, defaultName);
486     }
487 
getDefaultName(String[] rawName)488     private static String getDefaultName(String[] rawName) {
489         String name = ""; //$NON-NLS-1$
490 
491         for (int i = 0; true; i++) {
492             String part = rawName[i];
493 
494             name += new String(new char[] {part.charAt(0)}).toUpperCase() +
495                     part.substring(1).toLowerCase();
496 
497             if (i == (rawName.length - 1)) {
498                 break;
499             }
500             name += " "; //$NON-NLS-1$
501         }
502 
503         return name;
504     }
505 
getPropertyName(String[] rawName)506     private static String getPropertyName(String[] rawName) {
507         String name = rawName[0].toLowerCase();
508 
509         for (int i = 1; i < rawName.length; i++) {
510             String part = rawName[i];
511 
512             name += new String(new char[] {part.charAt(0)}).toUpperCase() +
513                     part.substring(1).toLowerCase();
514         }
515 
516         return ("AWT." + name); //$NON-NLS-1$
517     }
518 
getPublicStaticFinalIntFieldName(int value)519     private static String[] getPublicStaticFinalIntFieldName(int value) {
520         Field[] allFields = KeyEvent.class.getDeclaredFields();
521 
522         try {
523             for (Field field : allFields) {
524                 Class<?> ssalc = field.getType();
525                 int modifiers = field.getModifiers();
526 
527                 if (ssalc.isPrimitive() && ssalc.getName().equals("int") && //$NON-NLS-1$
528                         Modifier.isFinal(modifiers) && Modifier.isPublic(modifiers) &&
529                         Modifier.isStatic(modifiers))
530                 {
531                     if (field.getInt(null) == value){
532                         final String name = field.getName();
533                         final int prefixLength = name.indexOf("_") + 1;
534                         return name.substring(prefixLength).split("_"); //$NON-NLS-1$
535                     }
536                 }
537             }
538         } catch (Exception e) {
539             throw new RuntimeException(e);
540         }
541 
542         return null;
543     }
544 
545     @Deprecated
KeyEvent(Component src, int id, long when, int modifiers, int keyCode)546     public KeyEvent(Component src, int id,
547                     long when, int modifiers,
548                     int keyCode) {
549         this(src, id, when, modifiers, keyCode,
550                 (keyCode > (2 << 7) - 1) ? CHAR_UNDEFINED : (char) keyCode);
551     }
552 
KeyEvent(Component src, int id, long when, int modifiers, int keyCode, char keyChar)553     public KeyEvent(Component src, int id,
554                     long when, int modifiers,
555                     int keyCode, char keyChar) {
556         this(src, id, when, modifiers, keyCode, keyChar, KEY_LOCATION_UNKNOWN);
557     }
558 
KeyEvent(Component src, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)559     public KeyEvent(Component src, int id,
560                     long when, int modifiers,
561                     int keyCode, char keyChar,
562                     int keyLocation) {
563         super(src, id, when, modifiers);
564 
565         if (id == KEY_TYPED) {
566             if (keyCode != VK_UNDEFINED) {
567                 // awt.191=Invalid keyCode for KEY_TYPED event, must be VK_UNDEFINED
568                 throw new IllegalArgumentException(Messages.getString("awt.191")); //$NON-NLS-1$
569             }
570             if (keyChar == CHAR_UNDEFINED) {
571                 // awt.192=Invalid keyChar for KEY_TYPED event, can't be CHAR_UNDEFINED
572                 throw new IllegalArgumentException(Messages.getString("awt.192")); //$NON-NLS-1$
573             }
574         }
575 
576         if ((keyLocation < KEY_LOCATION_UNKNOWN)
577                 || (keyLocation > KEY_LOCATION_NUMPAD)) {
578             // awt.297=Invalid keyLocation
579             throw new IllegalArgumentException(Messages.getString("awt.297")); //$NON-NLS-1$
580         }
581 
582         this.keyChar = keyChar;
583         this.keyLocation = keyLocation;
584         this.keyCode = keyCode;
585     }
586 
getKeyCode()587     public int getKeyCode() {
588         return keyCode;
589     }
590 
setKeyCode(int keyCode)591     public void setKeyCode(int keyCode) {
592         this.keyCode = keyCode;
593     }
594 
getKeyChar()595     public char getKeyChar() {
596         return keyChar;
597     }
598 
setKeyChar(char keyChar)599     public void setKeyChar(char keyChar) {
600         this.keyChar = keyChar;
601     }
602 
getKeyLocation()603     public int getKeyLocation() {
604         return keyLocation;
605     }
606 
607     @Override
608     @Deprecated
setModifiers(int modifiers)609     public void setModifiers(int modifiers) {
610         super.setModifiers(modifiers);
611     }
612 
isActionKey()613     public boolean isActionKey() {
614         return ((keyChar == CHAR_UNDEFINED) && (keyCode != VK_UNDEFINED) &&
615                 !((keyCode == VK_ALT) || (keyCode == VK_ALT_GRAPH) ||
616                     (keyCode == VK_CONTROL) || (keyCode == VK_META) || (keyCode == VK_SHIFT)));
617     }
618 
619     @Override
paramString()620     public String paramString() {
621         /*
622          * The format is based on 1.5 release behavior
623          * which can be revealed by the following code:
624          *
625          * KeyEvent e = new KeyEvent(new Component() {},
626          *       KeyEvent.KEY_PRESSED, 0,
627          *       KeyEvent.CTRL_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK,
628          *       KeyEvent.VK_A, 'A', KeyEvent.KEY_LOCATION_STANDARD);
629          * System.out.println(e);
630          */
631 
632         String idString = null;
633         String locString = null;
634         String paramString = null;
635         String keyCharString = (keyChar == '\n') ?
636                 keyCharString = getKeyText(VK_ENTER) : "'" + keyChar + "'"; //$NON-NLS-1$ //$NON-NLS-2$
637 
638         switch (id) {
639         case KEY_PRESSED:
640             idString = "KEY_PRESSED"; //$NON-NLS-1$
641             break;
642         case KEY_RELEASED:
643             idString = "KEY_RELEASED"; //$NON-NLS-1$
644             break;
645         case KEY_TYPED:
646             idString = "KEY_TYPED"; //$NON-NLS-1$
647             break;
648         default:
649             idString = "unknown type"; //$NON-NLS-1$
650         }
651 
652         switch(keyLocation){
653         case KEY_LOCATION_STANDARD:
654             locString = "KEY_LOCATION_STANDARD"; //$NON-NLS-1$
655             break;
656         case KEY_LOCATION_LEFT:
657             locString = "KEY_LOCATION_LEFT"; //$NON-NLS-1$
658             break;
659         case KEY_LOCATION_RIGHT:
660             locString = "KEY_LOCATION_RIGHT"; //$NON-NLS-1$
661             break;
662         case KEY_LOCATION_NUMPAD:
663             locString = "KEY_LOCATION_NUMPAD"; //$NON-NLS-1$
664             break;
665         case KEY_LOCATION_UNKNOWN:
666             locString = "KEY_LOCATION_UNKNOWN"; //$NON-NLS-1$
667             break;
668         default:
669             locString = "unknown type"; //$NON-NLS-1$
670         }
671 
672         paramString = idString + ",keyCode=" + keyCode; //$NON-NLS-1$
673         if (isActionKey()) {
674             paramString += "," + getKeyText(keyCode); //$NON-NLS-1$
675         } else {
676             paramString += ",keyChar=" + keyCharString; //$NON-NLS-1$
677         }
678         if (getModifiersEx() > 0) {
679             paramString += ",modifiers=" + getModifiersExText(getModifiersEx()) + //$NON-NLS-1$
680                     ",extModifiers=" + getModifiersExText(getModifiersEx()); //$NON-NLS-1$
681         }
682         paramString += ",keyLocation=" + locString; //$NON-NLS-1$
683 
684         return paramString;
685     }
686 
687 }
688