Example usage for javax.swing KeyStroke getKeyCode

List of usage examples for javax.swing KeyStroke getKeyCode

Introduction

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

Prototype

public final int getKeyCode() 

Source Link

Document

Returns the numeric key code for this AWTKeyStroke .

Usage

From source file:no.java.swing.resource.ResourceMapImpl.java

@Override
public final Integer getKeyCode(String key) {
    KeyStroke stroke = getKeyStroke(key);
    return stroke != null ? stroke.getKeyCode() : null;
}

From source file:org.colombbus.tangara.commons.resinject.AbstractButtonInjecter.java

private void injectMnemonicKey() {
    String mnemonicKey = key + MNEMONIC_KEY_SUFFIX;
    if (classResource.containsKey(mnemonicKey)) {
        KeyStroke mnemonicValue = classResource.getKeyStroke(mnemonicKey);
        button.setMnemonic(mnemonicValue.getKeyCode());
    }//from w ww .j av  a2 s  .  c  om
}

From source file:org.colombbus.tangara.commons.resinject.ActionInjecter.java

private void injectMnemonicKey() {
    String mnemonicKey = actionKey + MNEMONIC_KEY_SUFFIX;
    if (classResource.containsKey(mnemonicKey)) {
        KeyStroke mnemonicKeyStroke = classResource.getKeyStroke(mnemonicKey);
        int keyEventValue = mnemonicKeyStroke.getKeyCode();
        action.putValue(Action.MNEMONIC_KEY, new Integer(keyEventValue));
    }/*from www  .  j  av a2 s  .  co  m*/
}

From source file:org.colombbus.tangara.commons.resinject.JMenuInjecter.java

private void injectMnemonicKey() {
    String mnemonicKey = key + MNEMONIC_KEY_SUFFIX;
    if (classResource.containsKey(mnemonicKey)) {
        KeyStroke mnemonicValue = classResource.getKeyStroke(mnemonicKey);
        menu.setMnemonic(mnemonicValue.getKeyCode());
    }/*from  www. ja v a 2  s  .c  om*/
}

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

/**
 * Types the given keystroke.//  ww w. ja  v  a  2  s .com
 * If any of the intercepting and event matching arguments are 
 * <code>null</code>, this method will not wait for event confirmation. It 
 * will simply assume that the events were received correctly. Otherwise,
 * this method will use the given interceptor and event matcher arguments to
 * handle event confirmation.
 * 
 * @param keyStroke The key stroke. May not be null.
 * @param interceptor The interceptor that will be used to wait for event
 *                    confirmation.
 * @param keyDownMatcher The event matcher to be used for key press event
 *                       confirmation.
 * @param keyUpMatcher The event matcher to be used for key release event
 *                     confirmation.
 */
public void type(KeyStroke keyStroke, IRobotEventInterceptor interceptor, IEventMatcher keyDownMatcher,
        IEventMatcher keyUpMatcher) {

    try {
        Validate.notNull(keyStroke);
        boolean waitForConfirm = interceptor != null && keyDownMatcher != null && keyUpMatcher != null;
        InterceptorOptions options = new InterceptorOptions(new long[] { AWTEvent.KEY_EVENT_MASK });
        List keycodes = modifierKeyCodes(keyStroke);
        keycodes.add(new Integer(keyStroke.getKeyCode()));
        if (log.isDebugEnabled()) {
            String keyModifierText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
            String keyText = KeyEvent.getKeyText(keyStroke.getKeyCode());
            log.debug("Key stroke: " + keyStroke); //$NON-NLS-1$
            log.debug("Modifiers, Key: " + keyModifierText + ", " + keyText); //$NON-NLS-1$//$NON-NLS-2$
            log.debug("number of keycodes: " + keycodes.size()); //$NON-NLS-1$
        }
        m_robot.setAutoWaitForIdle(true);

        // FIXME Hack for MS Windows for keys that also appear on the numpad.
        //       Turns NumLock off. Does nothing if locking key functionality
        //       isn't implemented for the operating system.
        boolean isNumLockToggled = hackWindowsNumpadKeys1(keyStroke.getKeyCode());

        // first press all keys, then release all keys, but
        // avoid to press and release any key twice (even if perhaps alt
        // and meta should have the same keycode(??)
        Set alreadyDown = new HashSet();
        ListIterator i = keycodes.listIterator();
        try {
            while (i.hasNext()) {
                Integer keycode = (Integer) i.next();
                if (log.isDebugEnabled()) {
                    log.debug("trying to press: " + keycode); //$NON-NLS-1$
                }
                if (!alreadyDown.contains(keycode)) {
                    IRobotEventConfirmer confirmer = null;
                    if (waitForConfirm) {
                        confirmer = interceptor.intercept(options);
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("pressing: " + keycode); //$NON-NLS-1$
                    }
                    alreadyDown.add(keycode);
                    m_robot.keyPress(keycode.intValue());
                    if (waitForConfirm) {
                        confirmer.waitToConfirm(null, keyDownMatcher);
                    }
                }
            }
        } finally {
            releaseKeys(options, alreadyDown, i, interceptor, keyUpMatcher);
            // FIXME Hack for MS Windows for keys that also appear on the numpad.
            //       Turns NumLock back on, if necessary.
            if (isNumLockToggled) {
                hackWindowsNumpadKeys2();
            }
        }
    } catch (IllegalArgumentException e) {
        throw new RobotException(e);
    }
}

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
    {//from   ww  w.j a  va 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}.
 *//*www  . j av a  2s  . c  om*/
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}.
 *///from  ww  w. ja v  a 2  s.  com
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.paxml.selenium.rc.SeleniumHelper.java

static String[] getKeyCodes(char ch) {
    boolean shiftRequired = false;
    String key = String.valueOf(ch);
    String value = KEY_CODES_MAP.get(key);

    if (value != null) {
        shiftRequired = true;/*from   w  ww . j a v a 2  s  . com*/
        key = value;
    } else if (Character.isUpperCase(key.charAt(0))) {
        shiftRequired = true;
    } else {
        key = key.toUpperCase();
    }

    KeyStroke ks = KeyStroke.getKeyStroke("pressed " + key.toUpperCase());
    int keyCode = 0;
    if (ks != null && ks.getKeyCode() != 0) {
        keyCode = ks.getKeyCode();
    } else {
        keyCode = CHAR_TO_KEY_CODE_MAP.get(key);
    }

    return shiftRequired ? new String[] { String.valueOf(KeyEvent.VK_SHIFT), String.valueOf(keyCode) }
            : new String[] { String.valueOf(keyCode) };
}

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 ww  . j  ava 2  s .c om*/

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

                acceleratorString += keyText;
            }

        }

    }

    return acceleratorString;
}