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:com.maddyhome.idea.vim.helper.StringHelper.java

@NotNull
public static String escape(@NotNull List<KeyStroke> keys) {
    final StringBuffer res = new StringBuffer();
    for (KeyStroke key : keys) {
        final char c = key.getKeyChar();
        final int modifiers = key.getModifiers();
        final int code = key.getKeyCode();
        if (c < ' ') {
            res.append('^').append((char) (c + 'A' - 1));
        } else if (c == '\n') {
            res.append("^J");
        } else if (c == '\t') {
            res.append("^I");
        } else if (c == '\u0000') {
            res.append("^@");
        } else if ((modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) {
            final char[] chars = Character.toChars(code);
            if (chars.length == 1) {
                res.append("^");
                res.append(chars);//from   w ww .j a  v  a2  s  .c o m
            }
        } else {
            res.append(c);
        }
    }
    return res.toString();
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }//  ww  w . j  a v a  2 s. c  o m
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }
    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }//from   ww  w  .  j  a v a2s. c  o  m
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

/**
 * Serializes a Keystrike to a String./*from w w  w .j  a  va  2s  .co m*/
 * It can then be fed into KeyStroke.getKeyStroke(String).
 *
 * See: http://www.exampledepot.com/egs/javax.swing/Key2Str.html
 */
public static String keyStroke2String(KeyStroke key) {
    StringBuffer s = new StringBuffer(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("shift ");
    }
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        s.append("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        s.append("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        s.append("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        s.append("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        s.append("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        if (key.getKeyChar() == Character.MAX_VALUE) {
            s.append("unknown-typed-key ");
            break;
        }
        s.append("typed ").append(key.getKeyChar()).append(" ");
        break;
    case KeyEvent.KEY_PRESSED:
        s.append("pressed ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    case KeyEvent.KEY_RELEASED:
        s.append("released ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:KeystrokeUtil.java

public static String keyStroke2String(KeyStroke key) {
    if (key == null)
        return "";
    StringBuilder s = new StringBuilder(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("Ctrl+");
    }//  ww  w. j  av  a  2 s .  com
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        s.append("Meta+");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        s.append("Alt+");
    }
    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("Shift+");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        s.append("Button1+");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        s.append("Button2+");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        s.append("Button3+");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        s.append(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
    case KeyEvent.KEY_RELEASED:
        s.append(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:Utilities.java

/** Converts a Swing key stroke descriptor to a familiar Emacs-like name.
* @param stroke key description/*ww  w  .  j  ava 2s . c  o  m*/
* @return name of the key (e.g. <code>CS-F1</code> for control-shift-function key one)
* @see #stringToKey
*/
public static String keyToString(KeyStroke stroke) {
    StringBuffer sb = new StringBuffer();

    // add modifiers that must be pressed
    if (addModifiers(sb, stroke.getModifiers())) {
        sb.append('-');
    }

    HashMap[] namesAndValues = initNameAndValues();

    String c = (String) namesAndValues[1].get(new Integer(stroke.getKeyCode()));

    if (c == null) {
        sb.append(stroke.getKeyChar());
    } else {
        sb.append(c);
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Returns the key stroke representation translated.
 * /*www  .ja  v  a 2  s.  c om*/
 * @param keyStroke The key stroke.
 * @param locale The locale.
 * @return The key stroke representation translated.
 */
public static String translate(KeyStroke keyStroke, Locale locale) {
    //      String[] tokens = StringUtils.parse(StringUtils.toString(keyStroke), " ");
    //      if (tokens.length > 1) {
    //         Arrays.sort(tokens, 0, tokens.length - 1);
    //      }
    //      StringBuilder b = new StringBuilder();
    //      for (int i = 0; i < tokens.length; i++) {
    //         String token = tokens[i];
    //         String key = "key_" + token;
    //         String translated = TextServer.getString(key, locale);
    //         if (translated.equals(TextServer.notFoundKey(key))) {
    //            translated = token;
    //         }
    //         if (i > 0) {
    //            b.append("+");
    //         }
    //         b.append(translated);
    //      }
    //      return b.toString();
    String acceleratorText = "";
    if (keyStroke != null) {
        int modifiers = keyStroke.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += "+";
        }
        acceleratorText += KeyEvent.getKeyText(keyStroke.getKeyCode());
    }
    return acceleratorText;
}

From source file:com.mirth.connect.client.ui.components.KeyStrokeTextField.java

public void setKeyStroke(KeyStroke keyStroke) {
    if (keyStroke != null) {
        keyCode = keyStroke.getKeyCode();
        modifiers = keyStroke.getModifiers();
        updateKeyStroke();/*from  w w  w  .  j  av  a  2  s  .  com*/
    } else {
        reset();
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFilterHelper.java

@Override
public void addShortcutListener(TextField textField, final ShortcutListener listener) {
    final JTextField dTextField = (JTextField) DesktopComponentsHelper.unwrap(textField);
    final KeyStroke keyStroke = DesktopComponentsHelper.convertKeyCombination(listener.getKeyCombination());
    dTextField.addKeyListener(new KeyAdapter() {
        @Override// w  w w  . j  a va2s  .  c  o  m
        public void keyReleased(KeyEvent e) {
            if (ObjectUtils.equals(e.getKeyCode(), keyStroke.getKeyCode())
                    && ObjectUtils.equals(e.getModifiers(), keyStroke.getModifiers())) {
                listener.handleShortcutPressed();
            }
        }
    });
}

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

/**
 * Types the given keystroke./*w  ww  . j a 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);
    }
}