Returns a unique text for the given key modifiers - Java Swing

Java examples for Swing:Key Event

Description

Returns a unique text for the given key modifiers

Demo Code

/* $Id$/*ww  w. j  a v a 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:
 */
//package com.java2s;
import java.awt.event.InputEvent;

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";

    /**
     * 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();
    }
}

Related Tutorials