Example #1
0
  /** Handles the event when the key is pressed */
  public void processKeyEvent(KeyEvent e) {
    int keyCode = e.getKeyCode();

    if (e.getID() == KeyEvent.KEY_PRESSED) {
      if (keyCode == KeyEvent.VK_Z) lockZoom();
      else if (keyCode == KeyEvent.VK_T) fixElTimePointer(currTime);
    } else super.processKeyEvent(e);
  }
  private void displayInfo(KeyEvent e, String keyStatus) {
    // Method copied from http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

    // You should only rely on the key char if the event
    // is a key typed event.
    int id = e.getID();
    String keyString;
    if (id == KeyEvent.KEY_TYPED) {
      char c = e.getKeyChar();
      keyString = "key character = '" + c + "'";
    } else {
      int keyCode = e.getKeyCode();
      keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
    }

    int modifiersEx = e.getModifiersEx();
    String modString = "extended modifiers = " + modifiersEx;
    String tmpString = KeyEvent.getModifiersExText(modifiersEx);
    if (tmpString.length() > 0) {
      modString += " (" + tmpString + ")";
    } else {
      modString += " (no extended modifiers)";
    }

    String actionString = "action key? ";
    if (e.isActionKey()) {
      actionString += "YES";
    } else {
      actionString += "NO";
    }

    String locationString = "key location: ";
    int location = e.getKeyLocation();
    if (location == KeyEvent.KEY_LOCATION_STANDARD) {
      locationString += "standard";
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
      locationString += "left";
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
      locationString += "right";
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
      locationString += "numpad";
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
      locationString += "unknown";
    }

    // Added:
    System.out.println("Keypress: " + keyString);
  }
  /**
   * Need this method to detect when the focus may have chance to leave the focus cycle root which
   * is EmbeddedFrame. Mostly, the code here is copied from
   * DefaultKeyboardFocusManager.processKeyEvent with some minor modifications.
   */
  public boolean dispatchKeyEvent(KeyEvent e) {

    // We can't guarantee that this is called on the same AppContext as EmbeddedFrame
    // belongs to. That's why we can't use public methods to find current focus cycle
    // root. Instead, we access KFM's private field directly.
    if (currentCycleRoot == null) {
      currentCycleRoot =
          (Field)
              AccessController.doPrivileged(
                  new PrivilegedAction() {
                    public Object run() {
                      try {
                        Field unaccessibleRoot =
                            KeyboardFocusManager.class.getDeclaredField("currentFocusCycleRoot");
                        if (unaccessibleRoot != null) {
                          unaccessibleRoot.setAccessible(true);
                        }
                        return unaccessibleRoot;
                      } catch (NoSuchFieldException e1) {
                        assert false;
                      } catch (SecurityException e2) {
                        assert false;
                      }
                      return null;
                    }
                  });
    }

    Container currentRoot = null;
    if (currentCycleRoot != null) {
      try {
        // The field is static, so we can pass null to Field.get() as the argument.
        currentRoot = (Container) currentCycleRoot.get(null);
      } catch (IllegalAccessException e3) {
        // This is impossible: currentCycleRoot would be null if setAccessible failed.
        assert false;
      }
    }

    // if we are not in EmbeddedFrame's cycle, we should not try to leave.
    if (this != currentRoot) {
      return false;
    }

    // KEY_TYPED events cannot be focus traversal keys
    if (e.getID() == KeyEvent.KEY_TYPED) {
      return false;
    }

    if (!getFocusTraversalKeysEnabled() || e.isConsumed()) {
      return false;
    }

    AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e);
    Set toTest;
    Component currentFocused = e.getComponent();

    Component last = getFocusTraversalPolicy().getLastComponent(this);
    toTest = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
    if (toTest.contains(stroke) && (currentFocused == last || last == null)) {
      if (traverseOut(FORWARD)) {
        e.consume();
        return true;
      }
    }

    Component first = getFocusTraversalPolicy().getFirstComponent(this);
    toTest = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
    if (toTest.contains(stroke) && (currentFocused == first || first == null)) {
      if (traverseOut(BACKWARD)) {
        e.consume();
        return true;
      }
    }
    return false;
  }