Java 8 KeyStroke - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 KeyStroke class.

This code example shows how to use the following methods:getKeyChar, getKeyCode, getModifiers, isOnKeyRelease
     * @see javax.swing.JComponent#processKeyBinding(javax.swing.KeyStroke, 
     * java.awt.event.KeyEvent, int, boolean) 
     */ 
    @Override 
    protected boolean processKeyBinding(final KeyStroke ks, final KeyEvent e, final int condition, final boolean pressed) {
    	if (super.processKeyBinding(ks, e, condition, pressed)) {
    		return true; 
    	} 
    	if(containsModifierKeyCode(ks))
    		return false; 
    	final FreeplaneMenuBar freeplaneMenuBar = Controller.getCurrentController().getViewController()
    	    .getFreeplaneMenuBar(); 
    	return freeplaneMenuBar.processKeyBinding(ks, e, JComponent.WHEN_IN_FOCUSED_WINDOW, pressed);
    } 
 
    private static final List<Integer> modifierKeyCodes = Arrays.asList(VK_CONTROL, VK_ALT, VK_SHIFT, VK_META, VK_WINDOWS);
	private boolean containsModifierKeyCode(final KeyStroke ks) {
		return modifierKeyCodes.contains(ks.getKeyCode());
	} 
} 

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyChar, getModifiers, isOnKeyRelease
 
	public static KeyStroke derive(final KeyStroke ks, final Character keyChar) {
		if (ks == null) {
			return ks;
		} 
		final int modifiers = ks.getModifiers();
		if (ks.getKeyChar() == KeyEvent.CHAR_UNDEFINED) {
			if (0 != (modifiers & KEY_MODIFIERS)) {
				switch (keyChar) {
					case '<': 
						return KeyStroke
						    .getKeyStroke(KeyEvent.VK_LESS, modifiers & ~KEY_MODIFIERS, ks.isOnKeyRelease());
					case '>': 
						return KeyStroke.getKeyStroke(KeyEvent.VK_GREATER, modifiers & ~KEY_MODIFIERS, ks
						    .isOnKeyRelease());
					case '+': 
						return KeyStroke
						    .getKeyStroke(KeyEvent.VK_PLUS, modifiers & ~KEY_MODIFIERS, ks.isOnKeyRelease());
					case '-': 
						return KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, modifiers & ~KEY_MODIFIERS, ks

5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyCode, getModifiers
     * @param k keystroke 
     * @return true if event matches the keystroke definition 
     */ 
    public static boolean matches(KeyEvent e, KeyStroke k){
        final int modifiersEx = e.getModifiersEx()  | e.getModifiers();// Hack to get full modifier value
        return e.getKeyCode() == k.getKeyCode() && modifiersEx == k.getModifiers();
    } 
} 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyCode, getModifiers
        } 
        play(resolver.getWindow().getFocusOwner());
    } 
 
    public void play(Component component) {
        FireableKeyEvent event = new FireableKeyEvent(component, keyStroke.getModifiers());
        event.fire(keyStroke.getKeyCode(), keyChar);
        component.repaint();
        AWTSync.sync(); 
    } 
 
    public String toScriptCode() {
        return scriptModel.getScriptCodeForKeystroke(keyChar, keyStroke, getComponentId(),
                KeyStrokeParser.getTextForKeyChar(keyChar));
    } 
 
    public int hashCode() { 
        return keyStroke.hashCode();
    } 
 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getModifiers
    when(maps.charFor(removeModifiersExceptShift(keyStroke))).thenReturn(null);
    assertThat(KeyStrokeMap.charFor(keyStroke)).isEqualTo(CHAR_UNDEFINED);
  } 
 
  private static KeyStroke removeModifiersExceptShift(KeyStroke base) {
    final int mask = base.getModifiers() & ~SHIFT_MASK;
    return KeyStroke.getKeyStroke(VK_A, mask);
  } 
} 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:toString
  } 
 
  @Override 
  public String toString() {
    String format = "%s[character='%s', keyStroke=%s]";
    return String.format(format, getClass().getSimpleName(), String.valueOf(character), keyStroke.toString());
  } 
} 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyCode, getModifiers
    /** Return a good string representation of the given keystroke, since 
     *  the toString method returns more garbage than we want to see in a  
     *  user interface. 
     */ 
    public static String keyStrokeToString(KeyStroke key) {
	int modifiers = key.getModifiers();
	StringBuffer buffer = new StringBuffer();
	if((modifiers & Event.SHIFT_MASK) == Event.SHIFT_MASK) {
	    buffer.append("(Shift-");
	    buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
	    buffer.append(")");
	}  
	if((modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) {
	    buffer.append("(Ctrl-");
	    buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
	    buffer.append(")");
	}  
	if((modifiers & Event.META_MASK) == Event.META_MASK) {
	    buffer.append("(Meta-");
	    buffer.append(KeyEvent.getKeyText(key.getKeyCode()));

4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyCode, getModifiers
        return name + " keyboard layout";
    } 
 
    public Gesture gestureForTyping(char ch) {
        KeyStroke keyStroke = keyStrokeFor(ch);
        return withModifierMask(keyStroke.getModifiers(), typeKey(keyStroke.getKeyCode()));
    } 
 
    private KeyStroke keyStrokeFor(char ch) {
        KeyStroke keyStroke = keyStrokes.get(ch);
        if (keyStroke != null) {
            return keyStroke;
        } else { 
            throw new IllegalArgumentException("no stroke available for character '" + ch + "'");
        } 
    } 
 
    /** 
     * Returns the default keyboard layout.  The name of the layout is specified by the 
     * com.objogate.wl.keyboard system property.  If that is not set, the name of the default 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyCode, getModifiers
     */ 
    public static String getKeyStrokeDisplayableRepresentation(KeyStroke ks) {
    	if (ks == null)
    		return null; 
    	 
        int modifiers = ks.getModifiers();
        String keyText = KeyEvent.getKeyText(ks.getKeyCode());
 
        if(modifiers!=0) {
            return getModifiersDisplayableRepresentation(modifiers)+"+"+keyText;
        } 
        return keyText;
    } 
 
    /** 
     * Returns a String representations of the given modifiers bitwise mask, in the following format:<br> 
     * <code>modifier+...+modifier 
     * 
     * <p>The modifiers' order in the returned String tries to mimick the keyboard layout of the current platform as 
     * much as possible: 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getKeyCode, getModifiers
     */ 
    public static void configureActionMenuItem(JMenuItem item) {
    	item.setIcon(null);
 
    	KeyStroke stroke = item.getAccelerator();
    	if (stroke != null && stroke.getModifiers() == 0 &&
    			(stroke.getKeyCode() == KeyEvent.VK_ENTER || stroke.getKeyCode() == KeyEvent.VK_SPACE || stroke.getKeyCode() == KeyEvent.VK_ESCAPE))
    		item.setAccelerator(null);
    } 
 
    public static JMenuItem addMenuItem(JMenu menu, MuAction action, MnemonicHelper mnemonicHelper) {
        return addMenuItem(menu, action, mnemonicHelper, false);
    } 
 
 
    public static JCheckBoxMenuItem addCheckBoxMenuItem(JMenu menu, MuAction action, MnemonicHelper mnemonicHelper) {
        return (JCheckBoxMenuItem)addMenuItem(menu, action, mnemonicHelper, true);
    } 
     
 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
See Code Examples for Java 8 KeyStroke Methods: