Example usage for javax.swing KeyStroke getModifiers

List of usage examples for javax.swing KeyStroke getModifiers

Introduction

In this page you can find the example usage for javax.swing KeyStroke getModifiers.

Prototype

public final int getModifiers() 

Source Link

Document

Returns the modifier keys for this AWTKeyStroke .

Usage

From source file:org.eclipse.jubula.rc.common.driver.KeyTyper.java

/**
 * @param keyStroke KeyStroke whose modifiers are requested
 * @return a List of KeyCodes (hopefully) realising the ModifierMask contained in the KeyStroke
 *//*from  ww  w .j  ava  2s . co m*/
private List modifierKeyCodes(KeyStroke keyStroke) {
    List l = new LinkedList();
    int modifiers = keyStroke.getModifiers();
    // this is jdk 1.3 - code.
    // use ALT_DOWN_MASK instead etc. with jdk 1.4 !
    if ((modifiers & InputEvent.ALT_MASK) != 0) {
        l.add(new Integer(KeyEvent.VK_ALT));
    }
    if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
        l.add(new Integer(KeyEvent.VK_ALT_GRAPH));
    }
    if ((modifiers & InputEvent.CTRL_MASK) != 0) {
        l.add(new Integer(KeyEvent.VK_CONTROL));
    }
    if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
        l.add(new Integer(KeyEvent.VK_SHIFT));
    }
    if ((modifiers & InputEvent.META_MASK) != 0) {
        l.add(new Integer(KeyEvent.VK_META));
    }
    return l;
}

From source file:org.eclipse.jubula.rc.swt.driver.RobotSwtImpl.java

/**
 * //w  w  w  .  ja  v  a 2 s.c o  m
 * @param keyStroke The key stroke. 
 * @return a list of key typers capable of generating the necessary
 *         events to simulate the modifiers of the given key stroke.
 */
private List modifierKeyTypers(KeyStroke keyStroke) {
    List l = new LinkedList();
    int modifiers = keyStroke.getModifiers();
    // this is jdk 1.3 - code.
    // use ALT_DOWN_MASK instead etc. with jdk 1.4 !
    if ((modifiers & InputEvent.ALT_MASK) != 0) {
        l.add(new KeyCodeTyper(SWT.ALT));
    }
    if ((modifiers & InputEvent.CTRL_MASK) != 0) {
        l.add(new KeyCodeTyper(SWT.CTRL));
    }
    if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
        l.add(new KeyCodeTyper(SWT.SHIFT));
    }
    if ((modifiers & InputEvent.META_MASK) != 0) {
        l.add(new KeyCodeTyper(SWT.COMMAND));
    }
    return l;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.accelerator.KeyStrokePropertyEditor.java

@Override
protected void openDialog(Property property) throws Exception {
    KeyStrokeDialog dialog = new KeyStrokeDialog(property.getTitle());
    // set initial KeyStroke
    {/*w  w  w  .j  a v a 2 s. c  om*/
        Object value = property.getValue();
        if (value instanceof KeyStroke) {
            KeyStroke keyStroke = (KeyStroke) value;
            int modifiers = keyStroke.getModifiers();
            int keyCode = keyStroke.getKeyCode();
            boolean onKeyRelease = keyStroke.isOnKeyRelease();
            dialog.setKeyStroke(KeyStroke.getKeyStroke(keyCode, modifiers, onKeyRelease));
        } else {
            dialog.setKeyStroke(KeyStroke.getKeyStroke(0, 0));
        }
    }
    // open dialog
    if (dialog.open() == Window.OK) {
        KeyStroke keyStroke = dialog.getKeyStroke();
        GenericProperty genericProperty = (GenericProperty) property;
        // prepare source
        String source = getKeyStrokeSource(keyStroke);
        // update source
        genericProperty.setExpression(source, Property.UNKNOWN_VALUE);
    }
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.accelerator.KeyStrokePropertyEditor.java

/**
 * @return the textual presentation of given {@link KeyStroke}.
 *//*from   w w w .j a  v a2s.c  o  m*/
private static String getText(KeyStroke stroke) {
    try {
        // prepare modifiers
        String modifiersText = "";
        {
            int modifiers = stroke.getModifiers();
            if ((modifiers & CTRL_MASK) != 0) {
                modifiersText += "Ctrl+";
            }
            if ((modifiers & ALT_MASK) != 0) {
                modifiersText += "Alt+";
            }
            if ((modifiers & SHIFT_MASK) != 0) {
                modifiersText += "Shift+";
            }
            if ((modifiers & META_MASK) != 0) {
                modifiersText += "Meta+";
            }
            if ((modifiers & ALT_GRAPH_MASK) != 0) {
                modifiersText += "AltGr+";
            }
            // remove trailing '+'
            if (modifiersText.length() != 0) {
                modifiersText = StringUtils.substring(modifiersText, 0, -1);
            }
        }
        // add key
        int keyCode = stroke.getKeyCode();
        if (keyCode != KeyEvent.VK_UNDEFINED) {
            return modifiersText + "-" + getKeyName(keyCode);
        }
        // no key
        return modifiersText;
    } catch (Throwable e) {
        DesignerPlugin.log(e);
    }
    return null;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.accelerator.KeyStrokePropertyEditor.java

/**
 * @return the source for given {@link KeyStroke}.
 *//* w w  w.j  a  v  a2 s  .co m*/
private static String getKeyStrokeSource(KeyStroke keyStroke) {
    // prepare modifiers source
    String modifiersSource = "";
    {
        int modifiers = keyStroke.getModifiers();
        if ((modifiers & CTRL_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.CTRL_MASK | ";
        }
        if ((modifiers & ALT_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.ALT_MASK | ";
        }
        if ((modifiers & SHIFT_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.SHIFT_MASK | ";
        }
        if ((modifiers & META_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.META_MASK | ";
        }
        if ((modifiers & ALT_GRAPH_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.ALT_GRAPH_MASK | ";
        }
        //
        if (modifiersSource.length() != 0) {
            modifiersSource = StringUtils.substring(modifiersSource, 0, -" | ".length());
        } else {
            modifiersSource = "0";
        }
    }
    // prepare key source
    String keyCodeSource;
    {
        String keyName = getKeyName(keyStroke.getKeyCode());
        if (keyName == null) {
            return null;
        }
        keyCodeSource = "java.awt.event.KeyEvent.VK_" + keyName;
    }
    // prepare source
    return "javax.swing.KeyStroke.getKeyStroke(" + keyCodeSource + ", " + modifiersSource + ")";
}

From source file:org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI.java

private String getAcceleratorString(JToolTip tip) {

    String acceleratorString = null;
    Action action = ((AbstractButton) tip.getComponent()).getAction();

    if (action != null) {

        KeyStroke keyStroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
        if (keyStroke != null) {

            int mod = keyStroke.getModifiers();
            acceleratorString = KeyEvent.getKeyModifiersText(mod);

            if (!MiscUtils.isNull(acceleratorString)) {

                acceleratorString += DELIMITER;
            }/*from w  w w  .jav  a 2  s  .  c o  m*/

            String keyText = KeyEvent.getKeyText(keyStroke.getKeyCode());
            if (!MiscUtils.isNull(keyText)) {

                acceleratorString += keyText;
            }

        }

    }

    return acceleratorString;
}

From source file:org.wings.plaf.css.FrameCG.java

private void appendStrokes(StringBuilder builder, SComponent component, int condition, InputMap inputMap) {
    KeyStroke[] keyStrokes = inputMap.keys();
    if (keyStrokes != null) {
        for (int i = 0; i < keyStrokes.length; i++) {
            KeyStroke keyStroke = keyStrokes[i];
            Object binding = inputMap.get(keyStroke);

            switch (keyStroke.getKeyEventType()) {
            case KeyEvent.KEY_PRESSED:
                builder.append("kss.push(new ks('");
                builder.append(component.getName());
                builder.append("',");
                builder.append(//from   w  w  w  .  j a v  a 2 s. c  om
                        condition == SComponent.WHEN_FOCUSED_OR_ANCESTOR_OF_FOCUSED_COMPONENT ? "!0" : "!1");
                builder.append(",'");
                builder.append(binding);
                builder.append("',");
                builder.append(keyStroke.getKeyCode());
                builder.append(',');
                builder.append((keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0 ? "!0" : "!1");
                builder.append(',');
                builder.append((keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0 ? "!0" : "!1");
                builder.append(',');
                builder.append((keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0 ? "!0" : "!1");
                builder.append("));\n");
                break;
            case KeyEvent.KEY_TYPED:
                break;
            case KeyEvent.KEY_RELEASED:
                break;
            }
        }
    }
}

From source file:org.wings.plaf.css.InputMapScriptListener.java

private static void writeMatch(StringBuffer buffer, KeyStroke keyStroke) {
    buffer.append("if (event.keyCode == " + keyStroke.getKeyCode());
    if ((keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0)
        buffer.append(" && event.shiftKey");
    else//w ww . j  a  va 2  s .c  o  m
        buffer.append(" && !event.shiftKey");
    if ((keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0)
        buffer.append(" && event.ctrlKey");
    else
        buffer.append(" && !event.ctrlKey");
    if ((keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0)
        buffer.append(" && event.altKey");
    else
        buffer.append(" && !event.altKey");
    buffer.append(")");
}