Formats a given KeyStroke into a string - Java Swing

Java examples for Swing:Key Event

Description

Formats a given KeyStroke into a string

Demo Code

/* $Id$//  ww w.  j  ava 2  s  .  c  o m
 *****************************************************************************
 * Copyright (c) 2009 Contributors - see below
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    mvw
 *****************************************************************************
 *
 * Some portions of this file was previously release using the BSD License:
 */
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import javax.swing.KeyStroke;

public class Main{
    /** 
     * The expression between modifier/modifier and between modifier/text 
     */
    public static final String MODIFIER_JOINER = " + ";
    /** 
     * The text for the shift modifier 
     */
    public static final String SHIFT_MODIFIER = "SHIFT";
    /** 
     * The text for the ctrl modifier 
     */
    public static final String CTRL_MODIFIER = "CTRL";
    /** 
     * The text for the meta modifier 
     */
    public static final String META_MODIFIER = "META";
    /** 
     * The text for the alt modifier 
     */
    public static final String ALT_MODIFIER = "ALT";
    /** 
     * The text for the alt-gr modifier 
     */
    public static final String ALT_GRAPH_MODIFIER = "altGraph";
    /**
     * Formats a given KeyStroke into a string
     * 
     * @param keyStroke         the KeyStroke to be formatted
     * @return                  the formatted String
     */
    public static String formatKeyStroke(KeyStroke keyStroke) {
        if (keyStroke != null) {
            return getModifiersText(keyStroke.getModifiers())
                    + KeyEventUtils.getKeyText(keyStroke.getKeyCode());
        } else {
            return "";
        }
    }
    /**
     * Returns a unique text for the given key modifiers 
     * 
     * @param modifiers   the modifiers to be "translated"
     * @return          the corrisponding text for the keyCode 
     */
    public static String getModifiersText(int modifiers) {
        StringBuffer buf = new StringBuffer();

        if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
            buf.append(SHIFT_MODIFIER).append(MODIFIER_JOINER);
        }
        if ((modifiers & InputEvent.CTRL_MASK) != 0) {
            buf.append(CTRL_MODIFIER).append(MODIFIER_JOINER);
        }
        if ((modifiers & InputEvent.META_MASK) != 0) {
            buf.append(META_MODIFIER).append(MODIFIER_JOINER);
        }
        if ((modifiers & InputEvent.ALT_MASK) != 0) {
            buf.append(ALT_MODIFIER).append(MODIFIER_JOINER);
        }
        if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
            buf.append(ALT_GRAPH_MODIFIER).append(MODIFIER_JOINER);
        }
        return buf.toString();
    }
    /**
     * Returns a unique text for a KeyEvent code 
     * 
     * @param keyCode   the keyCode to be "translated"
     * @return          the corrisponding text for the keyCode 
     */
    public static String getKeyText(int keyCode) {
        int expectedModifiers = (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);

        Field[] fields = KeyEvent.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            try {
                if (fields[i].getModifiers() == expectedModifiers
                        && fields[i].getType() == Integer.TYPE
                        && fields[i].getName().startsWith("VK_")
                        && fields[i].getInt(KeyEvent.class) == keyCode) {

                    return fields[i].getName().substring(3);
                }
            } catch (IllegalAccessException e) {

            }
        }
        return "UNKNOWN";
    }
}

Related Tutorials