Java Swing Key Action parseKeyStroke(String keyStroke)

Here you can find the source of parseKeyStroke(String keyStroke)

Description

Converts a string to a keystroke.

License

Open Source License

Parameter

Parameter Description
keyStroke A string description of the key stroke

Declaration

public static KeyStroke parseKeyStroke(String keyStroke) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  /*from   w w w.j a v  a 2s  .co  m*/
 *  Copyright (C) 2010 Jalian Systems Private Ltd.
 *  Copyright (C) 2010 Contributors to Marathon OSS Project
 * 
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 * 
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  Project website: http://www.marathontesting.com
 *  Help: Marathon help forum @ http://groups.google.com/group/marathon-testing
 * 
 *******************************************************************************/

import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Converts a string to a keystroke. The string should be of the form
     * <i>modifiers</i>+<i>shortcut</i> where <i>modifiers</i> is any
     * combination of A for Alt, C for Control, S for Shift or M for Meta, and
     * <i>shortcut</i> is either a single character, or a keycode name from the
     * <code>KeyEvent</code> class, without the <code>VK_</code> prefix. Using ^
     * for modifier uses Platform specific Menu Shortcut mask.
     * 
     * @param keyStroke
     *            A string description of the key stroke
     */
    public static KeyStroke parseKeyStroke(String keyStroke) {
        Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        if (keyStroke == null)
            return null;
        int modifiers = 0;
        int index = keyStroke.indexOf('+');
        if (index != -1) {
            for (int i = 0; i < index; i++) {
                switch (Character.toUpperCase(keyStroke.charAt(i))) {
                case 'A':
                    modifiers |= InputEvent.ALT_MASK;
                    break;
                case 'C':
                    modifiers |= InputEvent.CTRL_MASK;
                    break;
                case 'M':
                    modifiers |= InputEvent.META_MASK;
                    break;
                case 'S':
                    modifiers |= InputEvent.SHIFT_MASK;
                    break;
                case '^':
                    modifiers |= Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
                    break;
                }
            }
        }
        String key = keyStroke.substring(index + 1);
        if (key.length() == 1) {
            char ch = Character.toUpperCase(key.charAt(0));
            if (modifiers == 0)
                return KeyStroke.getKeyStroke(ch);
            else
                return KeyStroke.getKeyStroke(ch, modifiers);
        } else if (key.length() == 0) {
            return null;
        } else {
            int ch;
            try {
                ch = KeyEvent.class.getField("VK_".concat(key)).getInt(null);
            } catch (Exception e) {
                return null;
            }
            return KeyStroke.getKeyStroke(ch, modifiers);
        }
    }
}

Related

  1. keyStrokeToString(KeyStroke key)
  2. keyStrokeToString(KeyStroke key)
  3. keyStrokeToString(KeyStroke key)
  4. makeCloseAction(final Window window)
  5. matches(Action action, KeyEvent ke)
  6. parseKeyStroke(String keyStroke)
  7. pressKey(Component component, int keyCode, int modifier)
  8. registerKey(JComponent component, final int keyEvent, int modifiers, Action action)
  9. registerKeyAction(JComponent component, int keyCode, Action action)