Java Code Examples for javax.swing.KeyStroke#equals()

The following examples show how to use javax.swing.KeyStroke#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StayOpenPopupMenu.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
    if (isReturnAction(e)) { // Handle SPACE and ENTER
        MenuElement[] p = manager.getSelectedPath();
        MenuElement m = p != null && p.length > 0 ? p[p.length - 1] : null;
        if (m instanceof StayOpen) {
            e.consume();
            if (e.getID() == KeyEvent.KEY_PRESSED)
                performAction((StayOpen)m, e.getModifiers());
            return;
        }
    } else for (Component component : getComponents()) { // Handle mnemonics and accelerators
        if (component instanceof StayOpen) {
            StayOpen item = (StayOpen)component;
            JMenuItem i = item.getItem();
            KeyStroke k = KeyStroke.getKeyStrokeForEvent(e);
            if (k.equals(mnemonic(i)) || k.equals(i.getAccelerator())) {
                e.consume();
                manager.setSelectedPath(new MenuElement[] { this, i });
                performAction(item, e.getModifiers());
                return;
            }
        }
    }
    
    super.processKeyEvent(e, path, manager);
}
 
Example 2
Source File: AnimationAutoCompletion.java    From 3Dscript with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Sets the keystroke that should be used to trigger the auto-complete popup
 * window.
 *
 * @param ks The keystroke.
 * @throws IllegalArgumentException If <code>ks</code> is <code>null</code>.
 * @see #getTriggerKey()
 */
@Override
public void setTriggerKey(KeyStroke ks) {
	if (ks == null) {
		throw new IllegalArgumentException("trigger key cannot be null");
	}
	if (!ks.equals(trigger)) {
		if (textComponent != null) {
			// Put old trigger action back.
			uninstallTriggerKey();
			// Grab current action for new trigger and replace it.
			installTriggerKey(ks);
		}
		trigger = ks;
	}
}
 
Example 3
Source File: PathEditor2.java    From coordination_oru with GNU General Public License v3.0 6 votes vote down vote up
private String getHelp() {
	String ret = "";
	TreeMap<String,String> helpText = new TreeMap<String, String>();
	for (KeyStroke key : panel.getInputMap().allKeys()) {
		if (key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_0,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_1,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_2,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_3,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_4,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_5,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_6,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_7,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_8,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_9,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_COMMA,0)))
			continue;
		if (key.getModifiers() != 0) helpText.put(key.toString().replaceAll("pressed ", "").replaceAll(" ", "-"), panel.getInputMap().get(key).toString());
		else helpText.put(key.toString().replaceAll("pressed ", ""), panel.getInputMap().get(key).toString());
	}
	for (Entry<String,String> en : helpText.entrySet()) {
		ret += en.getKey() + ": " + en.getValue() + "\n";
	}
	return ret;
}
 
Example 4
Source File: PathEditor.java    From coordination_oru with GNU General Public License v3.0 6 votes vote down vote up
private String getHelp() {
	String ret = "";
	TreeMap<String,String> helpText = new TreeMap<String, String>();
	for (KeyStroke key : panel.getInputMap().allKeys()) {
		if (key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_0,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_1,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_2,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_3,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_4,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_5,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_6,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_7,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_8,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_9,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,0)) ||
				key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_COMMA,0)))
			continue;
		if (key.getModifiers() != 0) helpText.put(key.toString().replaceAll("pressed ", "").replaceAll(" ", "-"), panel.getInputMap().get(key).toString());
		else helpText.put(key.toString().replaceAll("pressed ", ""), panel.getInputMap().get(key).toString());
	}
	for (Entry<String,String> en : helpText.entrySet()) {
		ret += en.getKey() + ": " + en.getValue() + "\n";
	}
	return ret;
}
 
Example 5
Source File: ShortcutListener.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addKeyStroke(KeyStroke keyStroke, boolean add) {
    String s = Utilities.keyToString(keyStroke, true);
    KeyStroke mappedStroke = Utilities.stringToKey(s);
    if (!keyStroke.equals(mappedStroke)) {
        return;
    }
    String k = KeyStrokeUtils.getKeyStrokeAsText(keyStroke);
    // check if the text can be mapped back
    if (key.equals("")) { //NOI18N
        textField.setText(k);
        if (add)
            key = k;
    } else {
        textField.setText(key + " " + k); //NOI18N
        if (add)
            key += " " + k; //NOI18N
    }
}
 
Example 6
Source File: TestAWTKeyStroke.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void checkSerializedKeyStrokes(int keyCode, int modifiers,
        boolean onKeyRelease) throws Exception {

    AWTKeyStroke awtKeyStroke = AWTKeyStroke.getAWTKeyStroke(
            keyCode, modifiers, onKeyRelease);

    KeyStroke keyStroke = KeyStroke.getKeyStroke(
            keyCode, modifiers, onKeyRelease);

    if (awtKeyStroke != getSerializedAWTKeyStroke(awtKeyStroke)) {
        throw new RuntimeException("Serialized AWTKeyStroke is not cached!");
    }

    awtKeyStroke = AWTKeyStroke.getAWTKeyStroke(
            keyCode, modifiers, !onKeyRelease);

    if (!keyStroke.equals(getSerializedAWTKeyStroke(keyStroke))) {
        throw new RuntimeException("Serialized KeyStroke is not cached!");
    }
}
 
Example 7
Source File: ShortcutEnterPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void keyPressed(KeyEvent e) {
    KeyStroke keyStroke = createKeyStroke(e);
    
    boolean add = e.getKeyCode() != e.VK_SHIFT &&
            e.getKeyCode() != e.VK_CONTROL &&
            e.getKeyCode() != e.VK_ALT &&
            e.getKeyCode() != e.VK_META &&
            e.getKeyCode() != e.VK_ALT_GRAPH;
    
    if (keyStroke.equals(backspaceKS) && !key.equals("")) {
        // delete last key
        int i = key.lastIndexOf(' ');
        if (i < 0)
            key = "";
        else
            key = key.substring(0, i);
        tfShortcut.setText(key);
    } else
        // add key
        addKeyStroke(keyStroke, add);
    
    e.consume();
}
 
Example 8
Source File: StayOpenPopupMenu.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
    if (isReturnAction(e)) { // Handle SPACE and ENTER
        MenuElement[] p = manager.getSelectedPath();
        MenuElement m = p != null && p.length > 0 ? p[p.length - 1] : null;
        if (m instanceof StayOpen) {
            e.consume();
            if (e.getID() == KeyEvent.KEY_PRESSED)
                performAction((StayOpen)m, e.getModifiers());
            return;
        }
    } else for (Component component : getComponents()) { // Handle mnemonics and accelerators
        if (component instanceof StayOpen) {
            StayOpen item = (StayOpen)component;
            JMenuItem i = item.getItem();
            KeyStroke k = KeyStroke.getKeyStrokeForEvent(e);
            if (k.equals(mnemonic(i)) || k.equals(i.getAccelerator())) {
                e.consume();
                manager.setSelectedPath(new MenuElement[] { this, i });
                performAction(item, e.getModifiers());
                return;
            }
        }
    }
    
    super.processKeyEvent(e, path, manager);
}
 
Example 9
Source File: MainMenuAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Adds accelerators to given JMenuItem taken from the action */
protected static void addAccelerators(Action a, JMenuItem item, JTextComponent target){
    if (target == null || a==null || item==null) return;
    
    // get accelerators from kitAction
    Action kitAction = getActionByName((String)a.getValue(Action.NAME));
    if (kitAction!=null) a = kitAction;
    // Try to get the accelerator, TopComponent action could be obsoleted
    Keymap km = target.getKeymap();

    if (km != null) {
        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        KeyStroke itemAccelerator = item.getAccelerator();
        
        if (keys != null && keys.length > 0) {
            if (itemAccelerator==null || !itemAccelerator.equals(keys[0])){
                item.setAccelerator(keys[0]);
            }
        }else{
            if (itemAccelerator!=null && kitAction!=null){
                item.setAccelerator(null);
            }
        }
    }
}
 
Example 10
Source File: AbbrevDetection.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkExpansionKeystroke(KeyEvent evt) {
    Position pos = null;
    Document d = null;
    synchronized (abbrevChars) {
        if (abbrevEndPosition != null && component != null && doc != null
            && component.getCaretPosition() == abbrevEndPosition.getOffset()
            && !isAbbrevDisabled()
            && doc.getProperty(EDITING_TEMPLATE_DOC_PROPERTY) == null
        ) {
            pos = abbrevEndPosition;
            d = component.getDocument();
        }
    }
    if (pos != null && d != null) {
        CodeTemplateManagerOperation operation = CodeTemplateManagerOperation.get(d, pos.getOffset());
        if (operation != null) {
            KeyStroke expandKeyStroke = operation.getExpansionKey();

            if (expandKeyStroke.equals(KeyStroke.getKeyStrokeForEvent(evt))) {
                if (expand(operation)) {
                    evt.consume();
                }
            }
        }
    }
}
 
Example 11
Source File: StyledLookAndFeel.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remap a swing default key binding to a native one, if needed.
 *
 * @param map keymap to be modified
 * @param key
 * @param defaultModifier swing default modifier key for the action
 * @param nativeModifier native modifier key for the action
 */
private void remapKey(InputMapUIResource map, int key, int defaultModifier,
		int nativeModifier) {
	KeyStroke defaultKey = KeyStroke.getKeyStroke(key, defaultModifier);
	Object action = map.get(defaultKey);

	KeyStroke nativeKey = KeyStroke.getKeyStroke(key, nativeModifier);
	if (!nativeKey.equals(defaultKey)) {
		map.remove(defaultKey);
		map.put(nativeKey, action);
	}
}
 
Example 12
Source File: ToolActions.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void propertyChange(PropertyChangeEvent evt) {
	if (!evt.getPropertyName().equals(DockingActionIf.KEYBINDING_DATA_PROPERTY)) {
		return;
	}

	DockingActionIf action = (DockingActionIf) evt.getSource();
	if (!action.getKeyBindingType().isManaged()) {
		// this reads unusually, but we need to notify the tool to rebuild its 'Window' menu 
		// in the case that this action is one of the tool's special actions
		keyBindingsChanged();
		return;
	}

	KeyBindingData newKeyBindingData = (KeyBindingData) evt.getNewValue();
	KeyStroke newKeyStroke = null;
	if (newKeyBindingData != null) {
		newKeyStroke = newKeyBindingData.getKeyBinding();
	}

	KeyStroke optKeyStroke = keyBindingOptions.getKeyStroke(action.getFullName(), null);
	if (newKeyStroke == null) {
		keyBindingOptions.removeOption(action.getFullName());
	}
	else if (!newKeyStroke.equals(optKeyStroke)) {
		keyBindingOptions.setKeyStroke(action.getFullName(), newKeyStroke);
		keyBindingsChanged();
	}
}
 
Example 13
Source File: CodeTemplateManagerOperation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static KeyStroke patchExpansionKey(KeyStroke eks) {
// Patch the keyPressed => keyTyped to prevent insertion of expand chars into editor
       if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0))) {
           eks = KeyStroke.getKeyStroke(' ');
       } else if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_MASK))) {
           eks = KeyStroke.getKeyStroke(new Character(' '), InputEvent.SHIFT_MASK);
       } else if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))) {
       } else if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0))) {
       }
return eks;
   }
 
Example 14
Source File: CodeTemplateManagerOperation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String getExpandKeyStrokeText(KeyStroke keyStroke) {
    String expandKeyStrokeText;
    if (keyStroke.equals(KeyStroke.getKeyStroke(' '))) { //NOI18N
        expandKeyStrokeText = "SPACE"; // NOI18N
    } else if (keyStroke.equals(KeyStroke.getKeyStroke(new Character(' '), InputEvent.SHIFT_MASK))) { //NOI18N
        expandKeyStrokeText = "Shift-SPACE"; // NOI18N
    } else if (keyStroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))) {
        expandKeyStrokeText = "TAB"; // NOI18N
    } else if (keyStroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0))) {
        expandKeyStrokeText = "ENTER"; // NOI18N
    } else {
        expandKeyStrokeText = keyStroke.toString();
    }
    return expandKeyStrokeText;
}
 
Example 15
Source File: ShortcutsDialog.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void keyPressed (KeyEvent e) {
    KeyStroke keyStroke = ShortcutListener.createKeyStroke(e);
    
    boolean add = e.getKeyCode () != KeyEvent.VK_SHIFT &&
                  e.getKeyCode () != KeyEvent.VK_CONTROL &&
                  e.getKeyCode () != KeyEvent.VK_ALT &&
                  e.getKeyCode () != KeyEvent.VK_META &&
                  e.getKeyCode () != KeyEvent.VK_ALT_GRAPH;
    
    if (keyStroke.equals (backspaceKS) && !key.equals ("")) {
        // delete last key
        int i = key.lastIndexOf (' '); //NOI18N
        if (i < 0) {
            key = ""; //NOI18N
        } else {
            key = key.substring (0, i);
        }
        getTfShortcut().setText (key);
    } else {
        // add key
        addKeyStroke (keyStroke, add);
    }
    if (add) {
        updateWarning();
    } else {
        setShortcutValid(false);
    }
    e.consume ();
}
 
Example 16
Source File: ShortcutListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void keyPressed(KeyEvent e) {
    assert (e.getSource() instanceof JTextField);

    if(((e.getModifiers() & (KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK | KeyEvent.CTRL_MASK | KeyEvent.META_MASK)) == 0) &&
            (e.getKeyCode() == KeyEvent.VK_DOWN || 
            e.getKeyCode() == KeyEvent.VK_UP ||
            e.getKeyCode() == KeyEvent.VK_ESCAPE)) {
        return ;
    }
    
    textField = (JTextField) e.getSource();
    KeyStroke keyStroke = createKeyStroke(e);

    boolean add = e.getKeyCode() != KeyEvent.VK_SHIFT &&
            e.getKeyCode() != KeyEvent.VK_CONTROL &&
            e.getKeyCode() != KeyEvent.VK_ALT &&
            e.getKeyCode() != KeyEvent.VK_META &&
            e.getKeyCode() != KeyEvent.VK_ALT_GRAPH;

    if (!(enterConfirms && keyStroke.equals(enterKS))) {
        if (keyStroke.equals(backspaceKS) && !key.equals("")) {
            // delete last key
            int i = key.lastIndexOf(' '); //NOI18N
            if (i < 0) {
                key = ""; //NOI18N
            } else {
                key = key.substring(0, i);
            }
            textField.setText(key);
        } else {
            // add key
            addKeyStroke(keyStroke, add);
        }

        e.consume();
    }
}
 
Example 17
Source File: TabPagerWidget.java    From radiance with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void uninstallMaps() {
    InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    InputMap newMap = new InputMap();
    if (currMap != null) {
        KeyStroke[] kss = currMap.allKeys();
        for (int i = 0; i < kss.length; i++) {
            KeyStroke stroke = kss[i];
            Object val = currMap.get(stroke);
            if (stroke
                    .equals(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_DOWN_MASK))) {
                if ("tabSwitcherForward".equals(val))
                    continue;
            }
            if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_DOWN_MASK))) {
                if ("tabSwitcherBackward".equals(val))
                    continue;
            }
            if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0, true))) {
                if ("tabSwitcherClose".equals(val))
                    continue;
            }
            if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0))) {
                if ("tabSwitcherHide".equals(val))
                    continue;
            }
            newMap.put(stroke, val);
        }
    }

    this.jcomp.getActionMap().remove("tabSwitcherForward");
    this.jcomp.getActionMap().remove("tabSwitcherBackward");
    this.jcomp.getActionMap().remove("tabSwitcherClose");
    this.jcomp.getActionMap().remove("tabSwitcherHide");

    SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
            newMap);
}
 
Example 18
Source File: MultiTabPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void keyTypedFromListWindow(KeyEvent e) {

		KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
		if (stroke.equals(NEXT_TAB_KEYSTROKE)) {
			nextProgramPressed();
		}
		else if (stroke.equals(PREVIOUS_TAB_KEYSTROKE)) {
			previousProgramPressed();
		}
	}
 
Example 19
Source File: FixedWatchesManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Action[] getActions (NodeActionsProvider original, Object node) 
throws UnknownTypeException {
    Action [] actions = original.getActions (node);
    List myActions = new ArrayList();
    if (fixedWatches.containsKey (new KeyWrapper(node))) {
        KeyStroke deleteKey = KeyStroke.getKeyStroke ("DELETE");
        int deleteIndex = -1;
        int editIndex = -1;
        for (int i = 0; i < actions.length; i++) {
            if (actions[i] != null) {
                if (deleteKey.equals(actions[i].getValue(Action.ACCELERATOR_KEY))) {
                    deleteIndex = i;
                }
                if (Boolean.TRUE.equals(actions[i].getValue("edit"))) {
                    editIndex = i;
                }
            }
        }
        if (deleteIndex >= 0) {
            actions = Arrays.copyOf(actions, actions.length);
            if (deleteIndex >= 0) {
                actions[deleteIndex] = DELETE_ACTION;
            }
        } else {
            myActions.add (0, DELETE_ACTION);
        }
        if (editIndex >= 0) {
            if (editIndex == actions.length - 1) {
                actions = Arrays.copyOf(actions, actions.length - 1);
            } else {
                Action[] actions2 = new Action[actions.length - 1];
                System.arraycopy(actions, 0, actions2, 0, editIndex);
                System.arraycopy(actions, editIndex + 1, actions2, editIndex, actions2.length - editIndex);
                actions = actions2;
            }
        }
    } else
    if (node instanceof Variable) {
        myActions.add (CREATE_FIXED_WATCH_ACTION);
    } else 
    if (node instanceof JPDAWatch) {
        myActions.add (CREATE_FIXED_WATCH_ACTION);
    } else 
        return actions;
    myActions.addAll (Arrays.asList (actions));
    return (Action[]) myActions.toArray (new Action [myActions.size ()]);
}