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;

import java.awt.Event;

import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;

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./*from   w ww . jav a 2s.c  o  m*/
     */
    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. isKeyStrokeEvent(@Nullable final KeyStroke keyStroke, final int keyEventType, @Nullable final KeyEvent event)
  2. isValidKey(int keyCode)
  3. keyEventPressed(KeyEvent event, KeyStroke keyStroke)
  4. keyStrokeToString(final KeyStroke keyStroke)
  5. keyStrokeToString(KeyStroke key)
  6. keyStrokeToString(KeyStroke key)
  7. makeCloseAction(final Window window)
  8. matches(Action action, KeyEvent ke)
  9. parseKeyStroke(String keyStroke)