Example usage for javax.swing KeyStroke getKeyEventType

List of usage examples for javax.swing KeyStroke getKeyEventType

Introduction

In this page you can find the example usage for javax.swing KeyStroke getKeyEventType.

Prototype

public final int getKeyEventType() 

Source Link

Document

Returns the type of KeyEvent which corresponds to this AWTKeyStroke .

Usage

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }/*from   w w  w .  ja va 2 s  . c  o  m*/
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }
    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }//  www. j a  va  2s.c o m
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

/**
 * Serializes a Keystrike to a String.// www . j av  a2s .  c  o  m
 * It can then be fed into KeyStroke.getKeyStroke(String).
 *
 * See: http://www.exampledepot.com/egs/javax.swing/Key2Str.html
 */
public static String keyStroke2String(KeyStroke key) {
    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:
        if (key.getKeyChar() == Character.MAX_VALUE) {
            s.append("unknown-typed-key ");
            break;
        }
        s.append("typed ").append(key.getKeyChar()).append(" ");
        break;
    case KeyEvent.KEY_PRESSED:
        s.append("pressed ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    case KeyEvent.KEY_RELEASED:
        s.append("released ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:KeystrokeUtil.java

public static String keyStroke2String(KeyStroke key) {
    if (key == null)
        return "";
    StringBuilder s = new StringBuilder(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("Ctrl+");
    }/*from w w  w .ja  va 2  s.  c  o  m*/
    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.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("Shift+");
    }
    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(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
    case KeyEvent.KEY_RELEASED:
        s.append(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:org.wings.plaf.css.FrameCG.java

private void appendStrokes(StringBuilder builder, SComponent component, int condition, InputMap inputMap) {
    KeyStroke[] keyStrokes = inputMap.keys();
    if (keyStrokes != null) {
        for (int i = 0; i < keyStrokes.length; i++) {
            KeyStroke keyStroke = keyStrokes[i];
            Object binding = inputMap.get(keyStroke);

            switch (keyStroke.getKeyEventType()) {
            case KeyEvent.KEY_PRESSED:
                builder.append("kss.push(new ks('");
                builder.append(component.getName());
                builder.append("',");
                builder.append(/*  ww w  .ja v  a2  s.  c  o m*/
                        condition == SComponent.WHEN_FOCUSED_OR_ANCESTOR_OF_FOCUSED_COMPONENT ? "!0" : "!1");
                builder.append(",'");
                builder.append(binding);
                builder.append("',");
                builder.append(keyStroke.getKeyCode());
                builder.append(',');
                builder.append((keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0 ? "!0" : "!1");
                builder.append(',');
                builder.append((keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0 ? "!0" : "!1");
                builder.append(',');
                builder.append((keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0 ? "!0" : "!1");
                builder.append("));\n");
                break;
            case KeyEvent.KEY_TYPED:
                break;
            case KeyEvent.KEY_RELEASED:
                break;
            }
        }
    }
}

From source file:org.wings.plaf.css.InputMapScriptListener.java

public static void install(SComponent component) {
    ScriptListener[] scriptListeners = component.getScriptListeners();

    for (int i = 0; i < scriptListeners.length; i++) {
        ScriptListener scriptListener = scriptListeners[i];
        if (scriptListener instanceof InputMapScriptListener)
            component.removeScriptListener(scriptListener);
    }//from  w  w w .  j a  v a  2  s  .  co m

    InputMap inputMap = component.getInputMap();
    if (inputMap.size() == 0)
        return; // we're done

    StringBuffer pressed = new StringBuffer();
    StringBuffer typed = new StringBuffer();
    StringBuffer released = new StringBuffer();
    KeyStroke[] keyStrokes = inputMap.keys();

    for (int i = 0; i < keyStrokes.length; i++) {
        KeyStroke keyStroke = keyStrokes[i];
        Object binding = inputMap.get(keyStroke);

        /*
        writeMatch(typed, keyStroke);
        writeRequest(typed, binding);
        */

        switch (keyStroke.getKeyEventType()) {
        case KeyEvent.KEY_PRESSED:
            writeMatch(pressed, keyStroke);
            writeRequest(pressed, binding);
            break;
        case KeyEvent.KEY_TYPED:
            writeMatch(typed, keyStroke);
            writeRequest(typed, binding);
            log.debug("typed binding = " + binding);
            break;
        case KeyEvent.KEY_RELEASED:
            writeMatch(released, keyStroke);
            writeRequest(released, binding);
            log.debug("released binding = " + binding);
            break;
        }
    }

    if (pressed.length() > 0)
        component.addScriptListener(
                new InputMapScriptListener("onkeydown", "return pressed_" + component.getName() + "(event)",
                        "function pressed_" + component.getName() + "(event) {\n  "
                                + "event = getEvent(event);\n  " + pressed.toString() + "  return true;\n}\n"));
    if (typed.length() > 0)
        component.addScriptListener(
                new InputMapScriptListener("onkeypress", "return typed_" + component.getName() + "(event)",
                        "function typed_" + component.getName() + "(event) {\n  "
                                + "event = getEvent(event);\n  " + typed.toString() + "  return true;\n}\n"));
    if (released.length() > 0)
        component.addScriptListener(
                new InputMapScriptListener("onkeyup", "return released_" + component.getName() + "(event)",
                        "function released_" + component.getName() + "(event) {\n"
                                + "event = getEvent(event);\n  " + released.toString()
                                + "  return true;\n}\n"));
}

From source file:org.wings.plaf.css.InputMapScriptListener.java

public static void installToFrame(SFrame frame, SComponent component) {

    InputMap inputMap = component.getInputMap(SComponent.WHEN_IN_FOCUSED_FRAME);

    StringBuffer pressed = new StringBuffer();
    StringBuffer typed = new StringBuffer();
    StringBuffer released = new StringBuffer();
    KeyStroke[] keyStrokes = inputMap.keys();

    for (int i = 0; i < keyStrokes.length; i++) {
        KeyStroke keyStroke = keyStrokes[i];
        Object binding = inputMap.get(keyStroke);

        /*/*w w w .j  av  a2s  .c  o  m*/
        writeMatch(typed, keyStroke);
        writeRequest(typed, binding);
        */

        switch (keyStroke.getKeyEventType()) {
        case KeyEvent.KEY_PRESSED:
            writeMatch(pressed, keyStroke);
            writeRequestForFrame(pressed, binding, component.getName());
            break;
        case KeyEvent.KEY_TYPED:
            writeMatch(typed, keyStroke);
            writeRequestForFrame(typed, binding, component.getName());
            log.debug("typed binding = " + binding);
            break;
        case KeyEvent.KEY_RELEASED:
            writeMatch(released, keyStroke);
            writeRequestForFrame(released, binding, component.getName());
            log.debug("released binding = " + binding);
            break;
        }
    }

    if (pressed.length() > 0)
        frame.addScriptListener(
                new InputMapScriptListener("onkeydown", "pressed_frame_" + component.getName() + "(event)",
                        "function pressed_frame_" + component.getName() + "(event) {\n  "
                                + "event = getEvent(event);\n  " + pressed.toString() + "  return true;\n}\n"));
    if (typed.length() > 0)
        frame.addScriptListener(
                new InputMapScriptListener("onkeypress", "typed_frame_" + component.getName() + "(event)",
                        "function typed_frame_" + component.getName() + "(event) {\n  "
                                + "event = getEvent(event);\n  " + typed.toString() + "  return true;\n}\n"));
    if (released.length() > 0)
        frame.addScriptListener(
                new InputMapScriptListener("onkeyup", "released_frame_" + component.getName() + "(event)",
                        "function released_frame_" + component.getName() + "(event) {\n"
                                + "event = getEvent(event);\n  " + released.toString()
                                + "  return true;\n}\n"));
}