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