Java Utililty Methods Swing Key Action

List of utility methods to do Swing Key Action

Description

The list of methods to do Swing Key Action are organized into topic(s).

Method

booleankeyEventPressed(KeyEvent event, KeyStroke keyStroke)
key Event Pressed
int targetKeyCode = keyStroke.getKeyCode();
return event.getKeyCode() == targetKeyCode && (event.getModifiers() & KeyEvent.CTRL_MASK) != 0;
StringkeyStrokeToString(final KeyStroke keyStroke)
Returns a nice string representation of the specified key stroke.
final StringBuilder sb = new StringBuilder(" (");
if (keyStroke.getModifiers() != 0)
    sb.append(KeyEvent.getKeyModifiersText(keyStroke.getModifiers())).append('+');
sb.append(KeyEvent.getKeyText(keyStroke.getKeyCode())).append(')');
return sb.toString();
StringkeyStrokeToString(KeyStroke key)
The KeyStroke.toString() method does not return a string that can be parsed by KeyStroke.getKeyStroke().
StringBuffer s = new StringBuffer(50);
int m = key.getModifiers();
if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
    s.append("shift ");
if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
    s.append("ctrl ");
if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
    s.append("meta ");
if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
    s.append("alt ");
if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
    s.append("button1 ");
if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
    s.append("button2 ");
if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
    s.append("button3 ");
switch (key.getKeyEventType()) {
case KeyEvent.KEY_TYPED:
    s.append("typed ");
    s.append(key.getKeyChar() + " ");
    break;
case KeyEvent.KEY_PRESSED:
    s.append("pressed ");
    s.append(getKeyText(key.getKeyCode()) + " ");
    break;
case KeyEvent.KEY_RELEASED:
    s.append("released ");
    s.append(getKeyText(key.getKeyCode()) + " ");
    break;
default:
    s.append("unknown-event-type ");
    break;
return s.toString();
StringkeyStrokeToString(KeyStroke key)
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.
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) {
...
StringkeyStrokeToString(KeyStroke key)
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.
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) {
...
ActionmakeCloseAction(final Window window)
make Close Action
return new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        closeWindowByEvent(window);
};
booleanmatches(Action action, KeyEvent ke)
Return whether a key event matches the mnemonic of a provided action.
KeyStroke key = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
return KeyStroke.getKeyStrokeForEvent(ke).equals(key);
KeyStrokeparseKeyStroke(String keyStroke)
Converts a string to a keystroke.
if (keyStroke == null) {
    return (null);
int modifiers = 0;
int ch = '\0';
int index = keyStroke.indexOf('+');
for (int i = 0; i < index; i++) {
    switch (Character.toUpperCase(keyStroke.charAt(i))) {
...
KeyStrokeparseKeyStroke(String keyStroke)
Converts a string to a 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))) {
...
voidpressKey(Component component, int keyCode, int modifier)
press Key
if (component.getKeyListeners().length > 0) {
    KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, modifier, keyCode, (char) keyCode);
    for (int i = 0; i < component.getKeyListeners().length; i++) {
        KeyListener keyListener = component.getKeyListeners()[i];
        keyListener.keyPressed(event);
if (JComponent.class.isInstance(component)) {
...