@Override
 public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
   assert assertEDT();
   // ?: Are we already cancelled?
   // (Judging by Timer and TimerQueue code, we can theoretically be raced to be posted onto EDT
   // by TimerQueue,
   // due to some lag, unfair scheduling)
   if (_timer == null) {
     // -> Yes, so don't post the new RELEASED event.
     return;
   }
   // Stop Timer and clean.
   cancel();
   // Creating new KeyEvent (we've consumed the original).
   KeyEvent newEvent =
       new RepostedKeyEvent(
           (Component) _originalKeyEvent.getSource(),
           _originalKeyEvent.getID(),
           _originalKeyEvent.getWhen(),
           _originalKeyEvent.getModifiers(),
           _originalKeyEvent.getKeyCode(),
           _originalKeyEvent.getKeyChar(),
           _originalKeyEvent.getKeyLocation());
   // Posting to EventQueue.
   Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(newEvent);
   // System.out.println("Posted synthetic RELEASED [" + newEvent + "].");
 }
예제 #2
0
  public void actionPerformed(@NotNull AnActionEvent event) {
    if (logger.isDebugEnabled()) {
      logger.debug("actionPerformed=" + event);
    }
    if (!VimPlugin.isEnabled()) {
      return;
    }

    if (event.getInputEvent() instanceof KeyEvent) {
      KeyEvent ke = (KeyEvent) event.getInputEvent();
      Editor editor = event.getData(PlatformDataKeys.EDITOR);
      if (editor != null) {
        KeyStroke key = KeyStroke.getKeyStrokeForEvent(ke);
        KeyHandler.getInstance().handleKey(editor, key, event.getDataContext());
      } else {
        if (ExEntryPanel.getInstance().isActive()) {
          KeyEvent e =
              new KeyEvent(
                  ke.getComponent(),
                  ke.getID(),
                  ke.getWhen(),
                  ke.getModifiers(),
                  ke.getKeyCode(),
                  ke.getKeyChar(),
                  ke.getKeyLocation());
          ExEntryPanel.getInstance().processKey(e);
        }
      }
    }
  }
    public void serialize(RbSerializer serializer, ByteBuffer buffer, Object object) {
      KeyPressedEvent obj = (KeyPressedEvent) object;
      java.awt.event.KeyEvent src = obj.getSourceEvent();

      serializer.serialize(buffer, src.getKeyChar());
      serializer.serialize(buffer, src.getKeyCode());
      serializer.serialize(buffer, src.getKeyLocation());
      serializer.serialize(buffer, src.getID());
      serializer.serialize(buffer, src.getModifiersEx());
      serializer.serialize(buffer, src.getWhen());
    }
예제 #4
0
 private boolean proceedKeyEvent(KeyEvent event, KeyStroke stroke) {
   if (myInputMap.get(stroke) != null) {
     final Action action = myActionMap.get(myInputMap.get(stroke));
     if (action != null && action.isEnabled()) {
       action.actionPerformed(
           new ActionEvent(
               getContent(), event.getID(), "", event.getWhen(), event.getModifiers()));
       return true;
     }
   }
   return false;
 }
예제 #5
0
 /*
  * Post an ActionEvent to the target (on
  * keydown) and the item is enabled.
  * Returns true if there is an associated shortcut.
  */
 boolean handleShortcut(KeyEvent e) {
   MenuShortcut s =
       new MenuShortcut(e.getKeyCode(), (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
   // Fix For 6185151: Menu shortcuts of all menuitems within a menu
   // should be disabled when the menu itself is disabled
   if (s.equals(shortcut) && isItemEnabled()) {
     // MenuShortcut match -- issue an event on keydown.
     if (e.getID() == KeyEvent.KEY_PRESSED) {
       doMenuEvent(e.getWhen(), e.getModifiers());
     } else {
       // silently eat key release.
     }
     return true;
   }
   return false;
 }
예제 #6
0
  /**
   * Processes a key event forwarded from the <code>MenuSelectionManager</code> and changes the menu
   * selection, if necessary, by using <code>MenuSelectionManager</code>'s API.
   *
   * <p>Note: you do not have to forward the event to sub-components. This is done automatically by
   * the <code>MenuSelectionManager</code>.
   *
   * @param e a <code>KeyEvent</code>
   * @param path the <code>MenuElement</code> path array
   * @param manager the <code>MenuSelectionManager</code>
   */
  public void processKeyEvent(KeyEvent e, MenuElement path[], MenuSelectionManager manager) {
    MenuKeyEvent mke =
        new MenuKeyEvent(
            e.getComponent(),
            e.getID(),
            e.getWhen(),
            e.getModifiers(),
            e.getKeyCode(),
            e.getKeyChar(),
            path,
            manager);
    processMenuKeyEvent(mke);

    if (mke.isConsumed()) {
      e.consume();
    }
  }
예제 #7
0
  public static boolean notifyAction(
      final Action action,
      final KeyStroke keyStroke,
      final KeyEvent keyEvent,
      final Object sender,
      final int modifiers) {
    if (action == null || !action.isEnabled()) {
      return false;
    }
    Object command = action.getValue(Action.ACTION_COMMAND_KEY);
    if (command == null && !(action instanceof ActionProxy)) {
      char keyChar = keyEvent.getKeyChar();
      if (keyChar != KeyEvent.CHAR_UNDEFINED) {
        command = String.valueOf(keyChar);
      }
    }

    action.actionPerformed(
        new ActionEvent(
            sender, ActionEvent.ACTION_PERFORMED, (String) command, keyEvent.getWhen(), modifiers));

    return true;
  }
예제 #8
0
  /**
   * Redefine some keys behavior. We can't use key listeners, because we have to make something
   * AFTER standard keys processing.
   */
  @Override
  protected void processKeyEvent(KeyEvent e) {
    if (e.getID() != KeyEvent.KEY_PRESSED) {
      // key released
      super.processKeyEvent(e);
      return;
    }

    boolean processed = false;

    boolean mac = StaticUtils.onMacOSX();

    Document3 doc = getOmDocument();

    // non-standard processing
    if (isKey(e, KeyEvent.VK_TAB, 0)) {
      // press TAB when 'Use TAB to advance'
      if (controller.settings.isUseTabForAdvance()) {
        controller.nextEntry();
        processed = true;
      }
    } else if (isKey(e, KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK)) {
      // press Shift+TAB when 'Use TAB to advance'
      if (controller.settings.isUseTabForAdvance()) {
        controller.prevEntry();
        processed = true;
      }
    } else if (isKey(e, KeyEvent.VK_ENTER, 0)) {
      // press ENTER
      if (!controller.settings.isUseTabForAdvance()) {
        controller.nextEntry();
        processed = true;
      } else {
        processed = true;
      }
    } else if ((!mac && isKey(e, KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK))
        || (mac && isKey(e, KeyEvent.VK_ENTER, KeyEvent.META_MASK))) {
      // press Ctrl+ENTER (Cmd+Enter for MacOS)
      if (!controller.settings.isUseTabForAdvance()) {
        controller.prevEntry();
        processed = true;
      }
    } else if (isKey(e, KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK)) {
      // convert Shift+Enter event to straight enter key
      KeyEvent ke =
          new KeyEvent(e.getComponent(), e.getID(), e.getWhen(), 0, KeyEvent.VK_ENTER, '\n');
      super.processKeyEvent(ke);
      processed = true;
    } else if ((!mac && isKey(e, KeyEvent.VK_A, KeyEvent.CTRL_MASK))
        || (mac && isKey(e, KeyEvent.VK_A, KeyEvent.META_MASK))) {
      // handling Ctrl+A manually (Cmd+A for MacOS)
      setSelectionStart(doc.getTranslationStart());
      setSelectionEnd(doc.getTranslationEnd());
      processed = true;
    } else if (isKey(e, KeyEvent.VK_O, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK)) {
      // handle Ctrl+Shift+O - toggle orientation LTR-RTL
      controller.toggleOrientation();
      processed = true;
    } else if ((!mac && isKey(e, KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_MASK))
        || (mac && isKey(e, KeyEvent.VK_BACK_SPACE, KeyEvent.ALT_MASK))) {
      // handle Ctrl+Backspace (Alt+Backspace for MacOS)
      try {
        int offset = getCaretPosition();
        int prevWord = Utilities.getPreviousWord(this, offset);
        int c = Math.max(prevWord, doc.getTranslationStart());
        setSelectionStart(c);
        setSelectionEnd(offset);
        replaceSelection("");

        processed = true;
      } catch (BadLocationException ex) {
        // do nothing
      }
    } else if ((!mac && isKey(e, KeyEvent.VK_DELETE, KeyEvent.CTRL_MASK))
        || (mac && isKey(e, KeyEvent.VK_DELETE, KeyEvent.ALT_MASK))) {
      // handle Ctrl+Backspace (Alt+Delete for MacOS)
      try {
        int offset = getCaretPosition();
        int nextWord = Utilities.getNextWord(this, offset);
        int c = Math.min(nextWord, doc.getTranslationEnd());
        setSelectionStart(offset);
        setSelectionEnd(c);
        replaceSelection("");

        processed = true;
      } catch (BadLocationException ex) {
        // do nothing
      }
    } else if ((!mac && isKey(e, KeyEvent.VK_PAGE_UP, KeyEvent.CTRL_MASK))
        || (mac && isKey(e, KeyEvent.VK_PAGE_UP, KeyEvent.META_MASK))) {
      // Ctrl+PgUp - to the begin of document(Cmd+PgUp for MacOS)
      setCaretPosition(0);
      processed = true;
    } else if ((!mac && isKey(e, KeyEvent.VK_PAGE_DOWN, KeyEvent.CTRL_MASK))
        || (mac && isKey(e, KeyEvent.VK_PAGE_DOWN, KeyEvent.META_MASK))) {
      // Ctrl+PgDn - to the end of document(Cmd+PgDn for MacOS)
      setCaretPosition(getOmDocument().getLength());
      processed = true;
    }

    // leave standard processing if need
    if (processed) {
      e.consume();
    } else {
      if ((e.getModifiers() & (KeyEvent.CTRL_MASK | KeyEvent.META_MASK | KeyEvent.ALT_MASK)) == 0) {
        // there is no Alt,Ctrl,Cmd keys, i.e. it's char
        if (e.getKeyCode() != KeyEvent.VK_SHIFT) {
          // it's not a single 'shift' press
          checkAndFixCaret();
        }
      }
      super.processKeyEvent(e);
    }

    controller.showLengthMessage();

    // some after-processing catches
    if (!processed && e.getKeyChar() != 0) {
      switch (e.getKeyCode()) {
        case KeyEvent.VK_HOME:
        case KeyEvent.VK_END:
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_RIGHT:
        case KeyEvent.VK_UP:
        case KeyEvent.VK_DOWN:
          checkAndFixCaret();
      }
    }
  }
예제 #9
0
  /**
   * Searches the contact list when any key, different from "space", "+" or "-" is typed. Selects
   * the Contact name closest to the typed string. The time between two button presses is checked to
   * determine whether the user makes a new search or a continuous search. When user types the same
   * letter consecutively the search mechanism selects the next Contact name starting with the same
   * letter.
   */
  public void keyTyped(KeyEvent e) {

    // Nothing to do if the contact list is empty
    if (contactList.getModel().getSize() <= 0) return;

    long eventTimestamp = e.getWhen();
    char keyChar = e.getKeyChar();

    if (keyChar == ' ') {
      openOrCloseGroup();
    } else if (keyChar == '+') {
      openGroup();
    } else if (keyChar == '-') {
      closeGroup();
    } else {

      if ((lastTypedTimestamp - eventTimestamp) > 1000) {
        keyBuffer.delete(0, keyBuffer.length() - 1);
      }
      this.lastTypedTimestamp = eventTimestamp;
      this.keyBuffer.append(keyChar);

      boolean selectedSameLetterContact = false;

      int selectedIndex = this.contactList.getSelectedIndex();

      // Check if there's any selected contact node and get its name.
      if (selectedIndex != -1) {
        Object selectedObject = this.contactList.getSelectedValue();

        if (selectedObject instanceof MetaContact) {
          String selectedContactName = ((MetaContact) selectedObject).getDisplayName();

          if (selectedContactName != null) {
            selectedSameLetterContact =
                selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString());
          }
        } else if (selectedObject instanceof ConferenceChatContact) {
          String selectedContactName = ((ConferenceChatContact) selectedObject).getName();

          if (selectedContactName != null) {
            selectedSameLetterContact =
                selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString());
          }
        }
      }
      // The search starts from the beginning if:
      // 1) the newly entered character is different from the last one
      // or
      // 2) the currently selected contact starts with a different letter
      int contactIndex =
          contactList.getNextMatch(
              keyBuffer.toString(),
              (lastTypedKey != keyChar || !selectedSameLetterContact) ? 0 : selectedIndex + 1,
              Position.Bias.Forward);

      int currentlySelectedIndex = this.contactList.getSelectedIndex();

      if (currentlySelectedIndex != contactIndex && contactIndex != -1) {
        this.contactList.setSelectedIndex(contactIndex);
        currentlySelectedIndex = contactList.getSelectedIndex();
      }

      this.contactList.ensureIndexIsVisible(currentlySelectedIndex);

      this.lastTypedKey = keyChar;
    }
  }
  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;
    }
  }
예제 #12
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;
  }