Java Swing Key Action keyStrokeToString(KeyStroke key)

Here you can find the source of keyStrokeToString(KeyStroke key)

Description

Return a good string representation of the given keystroke, since the toString method returns more garbage than we want to see in a user interface.

License

Open Source License

Declaration

public static String keyStrokeToString(KeyStroke key) 

Method Source Code

//package com.java2s;
/*//from   w  ww  .  j a  v  a2  s. c  o  m
 Copyright (c) 1998-2013 The Regents of the University of California
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN  BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
 */

import java.awt.Event;

import java.awt.event.KeyEvent;

import javax.swing.KeyStroke;

public class Main {
    /** Return a good string representation of the given keystroke, since
     *  the toString method returns more garbage than we want to see in a
     *  user interface.
     */
    public static String keyStrokeToString(KeyStroke key) {
        int modifiers = key.getModifiers();
        StringBuffer buffer = new StringBuffer();

        if ((modifiers & Event.SHIFT_MASK) == Event.SHIFT_MASK) {
            buffer.append("(Shift-");
            buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
            buffer.append(")");
        }

        if ((modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) {
            buffer.append("(Ctrl-");
            buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
            buffer.append(")");
        }

        if ((modifiers & Event.META_MASK) == Event.META_MASK) {
            buffer.append("(Meta-");
            buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
            buffer.append(")");
        }

        if ((modifiers & Event.ALT_MASK) == Event.ALT_MASK) {
            buffer.append("(Alt-");
            buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
            buffer.append(")");
        }

        if (modifiers == 0) {
            buffer.append("(");
            buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
            buffer.append(")");
        }

        return buffer.toString();
    }
}

Related

  1. isValidKey(int keyCode)
  2. keyEventPressed(KeyEvent event, KeyStroke keyStroke)
  3. keyStrokeToString(final KeyStroke keyStroke)
  4. keyStrokeToString(KeyStroke key)
  5. keyStrokeToString(KeyStroke key)
  6. makeCloseAction(final Window window)
  7. matches(Action action, KeyEvent ke)
  8. parseKeyStroke(String keyStroke)
  9. parseKeyStroke(String keyStroke)