Beispiel #1
0
    public void processKeyEvent(KeyEvent evt) {
      evt = KeyEventWorkaround.processKeyEvent(evt);
      if (evt == null) return;

      switch (evt.getID()) {
        case KeyEvent.KEY_TYPED:
          char ch = evt.getKeyChar();
          if (!nonDigit && Character.isDigit(ch)) {
            super.processKeyEvent(evt);
            repeat = true;
            repeatCount = Integer.parseInt(action.getText());
          } else {
            nonDigit = true;
            if (repeat) {
              passToView(evt);
            } else super.processKeyEvent(evt);
          }
          break;
        case KeyEvent.KEY_PRESSED:
          int keyCode = evt.getKeyCode();
          if (evt.isActionKey()
              || evt.isControlDown()
              || evt.isAltDown()
              || evt.isMetaDown()
              || keyCode == KeyEvent.VK_BACK_SPACE
              || keyCode == KeyEvent.VK_DELETE
              || keyCode == KeyEvent.VK_ENTER
              || keyCode == KeyEvent.VK_TAB
              || keyCode == KeyEvent.VK_ESCAPE) {
            nonDigit = true;
            if (repeat) {
              passToView(evt);
              break;
            } else if (keyCode == KeyEvent.VK_TAB) {
              complete(true);
              evt.consume();
            } else if (keyCode == KeyEvent.VK_ESCAPE) {
              evt.consume();
              if (popup != null) {
                popup.dispose();
                popup = null;
                action.requestFocus();
              } else {
                if (temp) view.removeToolBar(ActionBar.this);
                view.getEditPane().focusOnTextArea();
              }
              break;
            } else if ((keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN)
                && popup != null) {
              popup.list.processKeyEvent(evt);
              break;
            }
          }
          super.processKeyEvent(evt);
          break;
      }
    }
  /**
   * Handle a key pressed event. This will look up the binding for the key stroke and execute it.
   */
  @Override
  @SuppressWarnings("unchecked")
  public void keyPressed(KeyEvent evt) {
    int keyCode = evt.getKeyCode();
    int modifiers = evt.getModifiers();

    if (keyCode == KeyEvent.VK_CONTROL
        || keyCode == KeyEvent.VK_SHIFT
        || keyCode == KeyEvent.VK_ALT
        || keyCode == KeyEvent.VK_META) return;

    if ((modifiers & InputEvent.SHIFT_MASK) != 0
        || (modifiers & InputEvent.CTRL_MASK) != 0
        || evt.isActionKey()
        || keyCode == KeyEvent.VK_BACK_SPACE
        || keyCode == KeyEvent.VK_DELETE
        || keyCode == KeyEvent.VK_ENTER
        || keyCode == KeyEvent.VK_TAB
        || keyCode == KeyEvent.VK_ESCAPE) {

      if (grabAction != null) {
        handleGrabAction(evt);
        return;
      }

      KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers);
      Object o = currentBindings.get(keyStroke);
      if (o == null) {
        // Don't beep if the user presses some
        // key we don't know about unless a
        // prefix is active. Otherwise it will
        // beep when caps lock is pressed, etc.
        if (currentBindings != bindings) {
          Toolkit.getDefaultToolkit().beep();
          // F10 should be passed on, but C+e F10
          // shouldn't
          repeatCount = 0;
          repeat = false;
          evt.consume();
        }
        currentBindings = bindings;
        return;
      } else if (o instanceof ActionListener) {
        currentBindings = bindings;

        executeAction(((ActionListener) o), evt.getSource(), null);

        evt.consume();
        return;
      } else if (o instanceof Hashtable) {
        currentBindings = (Hashtable) o;
        evt.consume();
        return;
      }
    }
  }
  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);
  }
  /**
   * Handle a key pressed event. This will look up the binding for the key stroke and execute it.
   */
  public void keyPressed(KeyEvent evt) {
    int keyCode = evt.getKeyCode();
    int modifiers = evt.getModifiers();

    // moved this earlier so it doesn't get random meta clicks
    if (keyCode == KeyEvent.VK_CONTROL
        || keyCode == KeyEvent.VK_SHIFT
        || keyCode == KeyEvent.VK_ALT
        || keyCode == KeyEvent.VK_META) {
      return;
    }

    // don't get command-s or other menu key equivs on mac
    // unless it's something that's specifically bound (cmd-left or right)
    // if ((modifiers & KeyEvent.META_MASK) != 0) return;
    if ((modifiers & KeyEvent.META_MASK) != 0) {
      KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers);
      if (currentBindings.get(keyStroke) == null) {
        return;
      }
    }

    /*
    char keyChar = evt.getKeyChar();
    System.out.println("code=" + keyCode + " char=" + keyChar +
                       " charint=" + ((int)keyChar));
    System.out.println("other codes " + KeyEvent.VK_ALT + " " +
                       KeyEvent.VK_META);
    */

    if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0
        || evt.isActionKey()
        || keyCode == KeyEvent.VK_BACK_SPACE
        || keyCode == KeyEvent.VK_DELETE
        || keyCode == KeyEvent.VK_ENTER
        || keyCode == KeyEvent.VK_TAB
        || keyCode == KeyEvent.VK_ESCAPE) {
      if (grabAction != null) {
        handleGrabAction(evt);
        return;
      }

      KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers);
      Object o = currentBindings.get(keyStroke);
      if (o == null) {
        // Don't beep if the user presses some
        // key we don't know about unless a
        // prefix is active. Otherwise it will
        // beep when caps lock is pressed, etc.
        if (currentBindings != bindings) {
          Toolkit.getDefaultToolkit().beep();
          // F10 should be passed on, but C+e F10
          // shouldn't
          repeatCount = 0;
          repeat = false;
          evt.consume();
        }
        currentBindings = bindings;
        return;
      } else if (o instanceof ActionListener) {
        currentBindings = bindings;

        executeAction(((ActionListener) o), evt.getSource(), null);

        evt.consume();
        return;
      } else if (o instanceof Hashtable) {
        currentBindings = (Hashtable) o;
        evt.consume();
        return;
      }
    }
  }
  public void keyTyped(KeyEvent e) {
    if (debug) {
      System.out.println(
          "--- RecordingModule: key typed = "
              + e
              + "\n  > Key char->int = "
              + (int) e.getKeyChar());
      System.out.println(" -- isActionKey() = " + e.isActionKey());
      System.out.println(" -- isISOControl() = " + Character.isISOControl(e.getKeyChar()));
      System.out.println(" -- isWhitespace() = " + Character.isWhitespace(e.getKeyChar()));
    }

    if (isKeyReserved(e)) {
      return;
    }

    if (enabled && !readOnly && lastKeyPressEvent != null) {

      if (enableKeyboard) {
        boolean replace = false;
        String text = "";
        if (isControl(e)) {
          if (lastKeyPressEvent.getKeyCode() == KeyEvent.VK_ENTER) {

            // Change the Type cmd prior to Typeline if the delay from the last type key is less
            // than 1 sec
            if (useTypeline && e.getModifiers() == 0 && lastElement != null) {
              String s = DocumentUtils.getElementText(lastElement);
              if (s.startsWith("Type ")
                  && (System.currentTimeMillis() - lastInsertTime) < typelineDelay) {
                replace = true;
                text = s.replaceFirst("Type", "Typeline");
              }
            }
          }

          if ("".equals(text)) {
            int count = 1;
            KeyEvent e2;

            long lastEventTime = e.getWhen();

            // We go through the vector of events and check whether there are events corresponding
            // to a typed text.
            for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) {
              e2 = (KeyEvent) events.get(i);
              if (e.getID() == e2.getID()
                  && e.getKeyChar() == e2.getKeyChar()
                  && e.getKeyCode() == e2.getKeyCode()
                  && e.getModifiers() == e2.getModifiers()
                  && (lastEventTime - e2.getWhen() < keyMutiDelay)) {
                count++;
                replace = true;
                lastEventTime = e2.getWhen();
              } else {
                break;
              }
            }

            text = "Press ";
            //                    String modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());
            String modifiers = parser.modifiersToString(e.getModifiers());
            if (!"".equals(modifiers)) {
              text += modifiers + "+";
            }
            String charText = KeyEvent.getKeyText(lastKeyPressEvent.getKeyCode());
            if (charText == null) {
              charText = "<unknown>";
            }
            text += charText;
            if (count > 1) {
              text += " " + PressCommand.PARAM_COUNT + "=" + count;
            }

            if (debug) {
              System.out.println("--- RecordingModule: Inserting '" + text + "'");
            }
          }

        } else {
          text = "" + e.getKeyChar();
          KeyEvent e2;

          // We go through the vector of events and check whether there are events corresponding to
          // a typed text.
          for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) {
            e2 = (KeyEvent) events.get(i);
            if (!isControl(e2) && !e2.isActionKey()) {
              text = e2.getKeyChar() + text;
              replace = true;
            } else {
              break;
            }
          }

          text = "Type \"" + Utils.escapeUnescapedDoubleQuotes(text) + "\"";
        }

        // Insert the command to the current editor
        insertLine(text, replace, true, false);
      }
      insertEvent(e);
    }
  }
  public void keyPressed(KeyEvent e) {

    if (debug) {
      System.out.println(
          "--- RecordingModule: key pressed = "
              + e
              + "\n  > Key char->int = "
              + (int) e.getKeyChar());
    }

    // Here we process just action keys because they do not generate KEY_TYPED events.
    // Other key events are handled by the keyTyped method.
    lastInteractionTime = System.currentTimeMillis();

    if (isKeyReserved(e)) {
      return;
    }

    if (enabled && !readOnly) {
      //        System.out.println("keyPressed (e.isActionKey()=)"+e.isActionKey()+":
      // "+e.toString());
      // TODO: implement text corrections in type like Delete, Backspace

      if (e.isActionKey()) {

        if (enableKeyboard) {
          int count = 1;
          KeyEvent e2;

          long lastEventTime = e.getWhen();

          // We go through the vector of events and check whether there are events corresponding to
          // a typed text.
          for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) {
            e2 = (KeyEvent) events.get(i);
            if (e.getID() == e2.getID()
                && e.getKeyChar() == e2.getKeyChar()
                && e.getKeyCode() == e2.getKeyCode()
                && e.getModifiers() == e2.getModifiers()
                && (lastEventTime - e2.getWhen() < keyMutiDelay)) {
              count++;
              lastEventTime = e2.getWhen();
            } else {
              break;
            }
          }

          String text = "Press ";
          //                String modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());
          String modifiers = parser.modifiersToString(e.getModifiers());
          if (!"".equals(modifiers)) {
            text += modifiers + "+";
          }
          String charText = (String) keyCodes.get(new Integer(e.getKeyCode()));
          if (charText == null) {
            charText = "<unknown>";
          }
          text += charText;
          if (count > 1) {
            text += " " + PressCommand.PARAM_COUNT + "=" + count;
          }
          //                text += '\n';

          if (debug) {
            System.out.println("--- RecordingModule: Inserting '" + text + "'");
          }

          // Insert the command to the current editor
          insertLine(text, count > 1, true, false);
        }
        insertEvent(e);
      }
      lastKeyPressEvent = e;
    }
  }
Beispiel #7
0
  /**
   * Converts a new event to an old one (used for compatibility). If the new event cannot be
   * converted (because no old equivalent exists) then this returns null.
   *
   * <p>Note: this method is here instead of in each individual new event class in java.awt.event
   * because we don't want to make it public and it needs to be called from java.awt.
   */
  Event convertToOld() {
    Object src = getSource();
    int newid = id;

    switch (id) {
      case KeyEvent.KEY_PRESSED:
      case KeyEvent.KEY_RELEASED:
        KeyEvent ke = (KeyEvent) this;
        if (ke.isActionKey()) {
          newid = (id == KeyEvent.KEY_PRESSED ? Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
        }
        int keyCode = ke.getKeyCode();
        if (keyCode == KeyEvent.VK_SHIFT
            || keyCode == KeyEvent.VK_CONTROL
            || keyCode == KeyEvent.VK_ALT) {
          return null; // suppress modifier keys in old event model.
        }
        // no mask for button1 existed in old Event - strip it out
        return new Event(
            src,
            ke.getWhen(),
            newid,
            0,
            0,
            Event.getOldEventKey(ke),
            (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));

      case MouseEvent.MOUSE_PRESSED:
      case MouseEvent.MOUSE_RELEASED:
      case MouseEvent.MOUSE_MOVED:
      case MouseEvent.MOUSE_DRAGGED:
      case MouseEvent.MOUSE_ENTERED:
      case MouseEvent.MOUSE_EXITED:
        MouseEvent me = (MouseEvent) this;
        // no mask for button1 existed in old Event - strip it out
        Event olde =
            new Event(
                src,
                me.getWhen(),
                newid,
                me.getX(),
                me.getY(),
                0,
                (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
        olde.clickCount = me.getClickCount();
        return olde;

      case FocusEvent.FOCUS_GAINED:
        return new Event(src, Event.GOT_FOCUS, null);

      case FocusEvent.FOCUS_LOST:
        return new Event(src, Event.LOST_FOCUS, null);

      case WindowEvent.WINDOW_CLOSING:
      case WindowEvent.WINDOW_ICONIFIED:
      case WindowEvent.WINDOW_DEICONIFIED:
        return new Event(src, newid, null);

      case ComponentEvent.COMPONENT_MOVED:
        if (src instanceof Frame || src instanceof Dialog) {
          Point p = ((Component) src).getLocation();
          return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
        }
        break;

      case ActionEvent.ACTION_PERFORMED:
        ActionEvent ae = (ActionEvent) this;
        String cmd;
        if (src instanceof Button) {
          cmd = ((Button) src).getLabel();
        } else if (src instanceof MenuItem) {
          cmd = ((MenuItem) src).getLabel();
        } else {
          cmd = ae.getActionCommand();
        }
        return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);

      case ItemEvent.ITEM_STATE_CHANGED:
        ItemEvent ie = (ItemEvent) this;
        Object arg;
        if (src instanceof List) {
          newid =
              (ie.getStateChange() == ItemEvent.SELECTED ? Event.LIST_SELECT : Event.LIST_DESELECT);
          arg = ie.getItem();
        } else {
          newid = Event.ACTION_EVENT;
          if (src instanceof Choice) {
            arg = ie.getItem();

          } else { // Checkbox
            arg = new Boolean(ie.getStateChange() == ItemEvent.SELECTED);
          }
        }
        return new Event(src, newid, arg);

      case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
        AdjustmentEvent aje = (AdjustmentEvent) this;
        switch (aje.getAdjustmentType()) {
          case AdjustmentEvent.UNIT_INCREMENT:
            newid = Event.SCROLL_LINE_DOWN;
            break;
          case AdjustmentEvent.UNIT_DECREMENT:
            newid = Event.SCROLL_LINE_UP;
            break;
          case AdjustmentEvent.BLOCK_INCREMENT:
            newid = Event.SCROLL_PAGE_DOWN;
            break;
          case AdjustmentEvent.BLOCK_DECREMENT:
            newid = Event.SCROLL_PAGE_UP;
            break;
          case AdjustmentEvent.TRACK:
            if (aje.getValueIsAdjusting()) {
              newid = Event.SCROLL_ABSOLUTE;
            } else {
              newid = Event.SCROLL_END;
            }
            break;
          default:
            return null;
        }
        return new Event(src, newid, new Integer(aje.getValue()));

      default:
    }
    return null;
  }