/** * Checks whether mouse event's button and modifiers match a shortcut configured in active keymap * for given action id. Only shortcuts having click count of 1 can be matched, mouse event's click * count is ignored. */ public static boolean isMouseActionEvent(@NotNull MouseEvent e, @NotNull String actionId) { KeymapManager keymapManager = KeymapManager.getInstance(); if (keymapManager == null) { return false; } Keymap keymap = keymapManager.getActiveKeymap(); if (keymap == null) { return false; } int button = MouseShortcut.getButton(e); int modifiers = e.getModifiersEx(); if (button == MouseEvent.NOBUTTON && e.getID() == MouseEvent.MOUSE_DRAGGED) { // mouse drag events don't have button field set due to some reason if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) { button = MouseEvent.BUTTON1; } else if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) { button = MouseEvent.BUTTON2; } } String[] actionIds = keymap.getActionIds(new MouseShortcut(button, modifiers, 1)); if (actionIds == null) { return false; } for (String id : actionIds) { if (actionId.equals(id)) { return true; } } return false; }
public static boolean isEmacsKeymap(@Nullable Keymap keymap) { for (; keymap != null; keymap = keymap.getParent()) { if ("Emacs".equalsIgnoreCase(keymap.getName())) { return true; } } return false; }
// ------------------------------------------------------------------ // from Java Swing 1.2 Orielly - Robert Eckstein // ------------------------------------------------------------------ protected JTextComponent updateKeymapForWord(JTextComponent textComp) { // create a new child keymap Keymap map = JTextComponent.addKeymap("NslmMap", textComp.getKeymap()); // define the keystrokeds to be added KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK, false); // add the new mappings used DefaultEditorKit actions map.addActionForKeyStroke(next, getAction(DefaultEditorKit.nextWordAction)); KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(prev, getAction(DefaultEditorKit.previousWordAction)); KeyStroke selNext = KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selNext, getAction(DefaultEditorKit.selectionNextWordAction)); KeyStroke selPrev = KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selPrev, getAction(DefaultEditorKit.selectionPreviousWordAction)); KeyStroke find = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(find, getAction("find")); KeyStroke findAgain = KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(findAgain, getAction("findAgain")); // set the keymap for the text component textComp.setKeymap(map); return (textComp); } // end updateKeymapForWord
/** * Checks that one of the mouse shortcuts assigned to the provided action has the same modifiers * as provided */ public static boolean matchActionMouseShortcutsModifiers( final Keymap activeKeymap, @JdkConstants.InputEventMask int modifiers, final String actionId) { final MouseShortcut syntheticShortcut = new MouseShortcut(MouseEvent.BUTTON1, modifiers, 1); for (Shortcut shortcut : activeKeymap.getShortcuts(actionId)) { if (shortcut instanceof MouseShortcut) { final MouseShortcut mouseShortcut = (MouseShortcut) shortcut; if (mouseShortcut.getModifiers() == syntheticShortcut.getModifiers()) { return true; } } } return false; }
// ------------------------------------------------------------------ // from Java Swing 1.2 Orielly - Robert Eckstein // ------------------------------------------------------------------ protected JTextComponent updateKeymapForEmacs(JTextComponent textComp) { // note: it does not look like a key can do more than one action // thus no modes. // todo: not all of these are correct. such as ctrlK // todo: add saving - ctrlXS // create a new child keymap Keymap map = JTextComponent.addKeymap("NslmMap", textComp.getKeymap()); KeyStroke selNext = KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selNext, getAction(DefaultEditorKit.selectionNextWordAction)); KeyStroke selPrev = KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selPrev, getAction(DefaultEditorKit.selectionPreviousWordAction)); KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(next, getAction(DefaultEditorKit.forwardAction)); KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(prev, getAction(DefaultEditorKit.backwardAction)); KeyStroke selectionDown = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selectionDown, getAction(DefaultEditorKit.downAction)); KeyStroke selectionUp = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selectionUp, getAction(DefaultEditorKit.upAction)); KeyStroke pageDown = KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(pageDown, getAction(DefaultEditorKit.pageDownAction)); KeyStroke pageUp = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(pageUp, getAction(DefaultEditorKit.pageUpAction)); KeyStroke endDoc = KeyStroke.getKeyStroke( KeyEvent.VK_GREATER, InputEvent.META_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(endDoc, getAction(DefaultEditorKit.endAction)); KeyStroke beginingDoc = KeyStroke.getKeyStroke( KeyEvent.VK_LESS, InputEvent.META_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(beginingDoc, getAction(DefaultEditorKit.beginAction)); // the VK_SPACE and VK_W not working as in Emacs - space deleting // KeyStroke // selectionStart=KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,InputEvent.CTRL_MASK,false); // map.addActionForKeyStroke(selectionStart,getAction(DefaultEditorKit.selectionForwardAction)); // //todo: setCharPosAction // this is doing nothing because only one char to can be assigned to cut // KeyStroke cut1=KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK,false); // map.addActionForKeyStroke(cut1,getAction(DefaultEditorKit.cutAction)); // if we do save as XS, this will have to change KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(cut, getAction(DefaultEditorKit.cutAction)); KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(paste, getAction(DefaultEditorKit.pasteAction)); KeyStroke moveToEndLine = KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(moveToEndLine, getAction(DefaultEditorKit.endLineAction)); // not emacs like KeyStroke selWord = KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selWord, getAction(DefaultEditorKit.selectWordAction)); KeyStroke selLine = KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selLine, getAction(DefaultEditorKit.selectLineAction)); KeyStroke delNext = KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(delNext, getAction(DefaultEditorKit.deleteNextCharAction)); KeyStroke insertLine = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(insertLine, getAction(DefaultEditorKit.insertBreakAction)); KeyStroke searchBackward = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(searchBackward, getAction("findAgain")); KeyStroke searchForward = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(searchForward, getAction("findAgain")); // set the keymap for the text component textComp.setKeymap(map); return (textComp); } // end updateKeymapForEmacs