333 */
334 public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) {
335 return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
336 false);
337 }
338
339 /**
340 * Returns an {@code AWTKeyStroke} which represents the
341 * stroke which generated a given {@code KeyEvent}.
342 * <p>
343 * This method obtains the keyChar from a {@code KeyTyped}
344 * event, and the keyCode from a {@code KeyPressed} or
345 * {@code KeyReleased} event. The {@code KeyEvent} modifiers are
346 * obtained for all three types of {@code KeyEvent}.
347 *
348 * @param anEvent the {@code KeyEvent} from which to
349 * obtain the {@code AWTKeyStroke}
350 * @throws NullPointerException if {@code anEvent} is null
351 * @return the {@code AWTKeyStroke} that precipitated the event
352 */
353 public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) {
354 int id = anEvent.getID();
355 switch(id) {
356 case KeyEvent.KEY_PRESSED:
357 case KeyEvent.KEY_RELEASED:
358 return getCachedStroke(KeyEvent.CHAR_UNDEFINED,
359 anEvent.getKeyCode(),
360 anEvent.getModifiers(),
361 (id == KeyEvent.KEY_RELEASED));
362 case KeyEvent.KEY_TYPED:
363 return getCachedStroke(anEvent.getKeyChar(),
364 KeyEvent.VK_UNDEFINED,
365 anEvent.getModifiers(),
366 false);
367 default:
368 // Invalid ID for this KeyEvent
369 return null;
370 }
371 }
372
380 * typedID := typed <typedKey>
381 * typedKey := string of length 1 giving Unicode character.
382 * pressedReleasedID := (pressed | released) key
383 * key := KeyEvent key code name, i.e. the name following "VK_".
384 * </pre>
385 * If typed, pressed or released is not specified, pressed is assumed. Here
386 * are some examples:
387 * <pre>
388 * "INSERT" => getAWTKeyStroke(KeyEvent.VK_INSERT, 0);
389 * "control DELETE" => getAWTKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
390 * "alt shift X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
391 * "alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
392 * "typed a" => getAWTKeyStroke('a');
393 * </pre>
394 *
395 * @param s a String formatted as described above
396 * @return an {@code AWTKeyStroke} object for that String
397 * @throws IllegalArgumentException if {@code s} is {@code null},
398 * or is formatted incorrectly
399 */
400 public static AWTKeyStroke getAWTKeyStroke(String s) {
401 if (s == null) {
402 throw new IllegalArgumentException("String cannot be null");
403 }
404
405 final String errmsg = "String formatted incorrectly";
406
407 StringTokenizer st = new StringTokenizer(s, " ");
408
409 int mask = 0;
410 boolean released = false;
411 boolean typed = false;
412 boolean pressed = false;
413
414 synchronized (AWTKeyStroke.class) {
415 if (modifierKeywords == null) {
416 Map<String, Integer> uninitializedMap = new HashMap<>(8, 1.0f);
417 uninitializedMap.put("shift",
418 Integer.valueOf(InputEvent.SHIFT_DOWN_MASK
419 |InputEvent.SHIFT_MASK));
691 assert(false);
692 }
693 }
694 return "UNKNOWN";
695 }
696
697 /**
698 * Returns a cached instance of {@code AWTKeyStroke} (or a subclass of
699 * {@code AWTKeyStroke}) which is equal to this instance.
700 *
701 * @return a cached instance which is equal to this instance
702 * @throws java.io.ObjectStreamException if a serialization problem occurs
703 */
704 protected Object readResolve() throws java.io.ObjectStreamException {
705 synchronized (AWTKeyStroke.class) {
706
707 return getCachedStroke(keyChar, keyCode, modifiers, onKeyRelease);
708 }
709 }
710
711 private static int mapOldModifiers(int modifiers) {
712 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
713 modifiers |= InputEvent.SHIFT_DOWN_MASK;
714 }
715 if ((modifiers & InputEvent.ALT_MASK) != 0) {
716 modifiers |= InputEvent.ALT_DOWN_MASK;
717 }
718 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
719 modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
720 }
721 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
722 modifiers |= InputEvent.CTRL_DOWN_MASK;
723 }
724 if ((modifiers & InputEvent.META_MASK) != 0) {
725 modifiers |= InputEvent.META_DOWN_MASK;
726 }
727
728 modifiers &= InputEvent.SHIFT_DOWN_MASK
729 | InputEvent.ALT_DOWN_MASK
730 | InputEvent.ALT_GRAPH_DOWN_MASK
731 | InputEvent.CTRL_DOWN_MASK
732 | InputEvent.META_DOWN_MASK
733 | InputEvent.BUTTON1_DOWN_MASK
734 | InputEvent.BUTTON2_DOWN_MASK
735 | InputEvent.BUTTON3_DOWN_MASK;
736
737 return modifiers;
738 }
739
740 private static int mapNewModifiers(int modifiers) {
741 if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
742 modifiers |= InputEvent.SHIFT_MASK;
743 }
744 if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
745 modifiers |= InputEvent.ALT_MASK;
746 }
747 if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
748 modifiers |= InputEvent.ALT_GRAPH_MASK;
749 }
750 if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
751 modifiers |= InputEvent.CTRL_MASK;
752 }
753 if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
754 modifiers |= InputEvent.META_MASK;
755 }
756
757 return modifiers;
758 }
759
|
333 */
334 public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) {
335 return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
336 false);
337 }
338
339 /**
340 * Returns an {@code AWTKeyStroke} which represents the
341 * stroke which generated a given {@code KeyEvent}.
342 * <p>
343 * This method obtains the keyChar from a {@code KeyTyped}
344 * event, and the keyCode from a {@code KeyPressed} or
345 * {@code KeyReleased} event. The {@code KeyEvent} modifiers are
346 * obtained for all three types of {@code KeyEvent}.
347 *
348 * @param anEvent the {@code KeyEvent} from which to
349 * obtain the {@code AWTKeyStroke}
350 * @throws NullPointerException if {@code anEvent} is null
351 * @return the {@code AWTKeyStroke} that precipitated the event
352 */
353 @SuppressWarnings("deprecation")
354 public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) {
355 int id = anEvent.getID();
356 switch(id) {
357 case KeyEvent.KEY_PRESSED:
358 case KeyEvent.KEY_RELEASED:
359 return getCachedStroke(KeyEvent.CHAR_UNDEFINED,
360 anEvent.getKeyCode(),
361 anEvent.getModifiers(),
362 (id == KeyEvent.KEY_RELEASED));
363 case KeyEvent.KEY_TYPED:
364 return getCachedStroke(anEvent.getKeyChar(),
365 KeyEvent.VK_UNDEFINED,
366 anEvent.getModifiers(),
367 false);
368 default:
369 // Invalid ID for this KeyEvent
370 return null;
371 }
372 }
373
381 * typedID := typed <typedKey>
382 * typedKey := string of length 1 giving Unicode character.
383 * pressedReleasedID := (pressed | released) key
384 * key := KeyEvent key code name, i.e. the name following "VK_".
385 * </pre>
386 * If typed, pressed or released is not specified, pressed is assumed. Here
387 * are some examples:
388 * <pre>
389 * "INSERT" => getAWTKeyStroke(KeyEvent.VK_INSERT, 0);
390 * "control DELETE" => getAWTKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
391 * "alt shift X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
392 * "alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
393 * "typed a" => getAWTKeyStroke('a');
394 * </pre>
395 *
396 * @param s a String formatted as described above
397 * @return an {@code AWTKeyStroke} object for that String
398 * @throws IllegalArgumentException if {@code s} is {@code null},
399 * or is formatted incorrectly
400 */
401 @SuppressWarnings("deprecation")
402 public static AWTKeyStroke getAWTKeyStroke(String s) {
403 if (s == null) {
404 throw new IllegalArgumentException("String cannot be null");
405 }
406
407 final String errmsg = "String formatted incorrectly";
408
409 StringTokenizer st = new StringTokenizer(s, " ");
410
411 int mask = 0;
412 boolean released = false;
413 boolean typed = false;
414 boolean pressed = false;
415
416 synchronized (AWTKeyStroke.class) {
417 if (modifierKeywords == null) {
418 Map<String, Integer> uninitializedMap = new HashMap<>(8, 1.0f);
419 uninitializedMap.put("shift",
420 Integer.valueOf(InputEvent.SHIFT_DOWN_MASK
421 |InputEvent.SHIFT_MASK));
693 assert(false);
694 }
695 }
696 return "UNKNOWN";
697 }
698
699 /**
700 * Returns a cached instance of {@code AWTKeyStroke} (or a subclass of
701 * {@code AWTKeyStroke}) which is equal to this instance.
702 *
703 * @return a cached instance which is equal to this instance
704 * @throws java.io.ObjectStreamException if a serialization problem occurs
705 */
706 protected Object readResolve() throws java.io.ObjectStreamException {
707 synchronized (AWTKeyStroke.class) {
708
709 return getCachedStroke(keyChar, keyCode, modifiers, onKeyRelease);
710 }
711 }
712
713 @SuppressWarnings("deprecation")
714 private static int mapOldModifiers(int modifiers) {
715 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
716 modifiers |= InputEvent.SHIFT_DOWN_MASK;
717 }
718 if ((modifiers & InputEvent.ALT_MASK) != 0) {
719 modifiers |= InputEvent.ALT_DOWN_MASK;
720 }
721 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
722 modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
723 }
724 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
725 modifiers |= InputEvent.CTRL_DOWN_MASK;
726 }
727 if ((modifiers & InputEvent.META_MASK) != 0) {
728 modifiers |= InputEvent.META_DOWN_MASK;
729 }
730
731 modifiers &= InputEvent.SHIFT_DOWN_MASK
732 | InputEvent.ALT_DOWN_MASK
733 | InputEvent.ALT_GRAPH_DOWN_MASK
734 | InputEvent.CTRL_DOWN_MASK
735 | InputEvent.META_DOWN_MASK
736 | InputEvent.BUTTON1_DOWN_MASK
737 | InputEvent.BUTTON2_DOWN_MASK
738 | InputEvent.BUTTON3_DOWN_MASK;
739
740 return modifiers;
741 }
742
743 @SuppressWarnings("deprecation")
744 private static int mapNewModifiers(int modifiers) {
745 if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
746 modifiers |= InputEvent.SHIFT_MASK;
747 }
748 if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
749 modifiers |= InputEvent.ALT_MASK;
750 }
751 if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
752 modifiers |= InputEvent.ALT_GRAPH_MASK;
753 }
754 if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
755 modifiers |= InputEvent.CTRL_MASK;
756 }
757 if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
758 modifiers |= InputEvent.META_MASK;
759 }
760
761 return modifiers;
762 }
763
|