Example usage for javax.swing InputMap keys

List of usage examples for javax.swing InputMap keys

Introduction

In this page you can find the example usage for javax.swing InputMap keys.

Prototype

public KeyStroke[] keys() 

Source Link

Document

Returns the KeyStroke s that are bound in this InputMap .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();

    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");

    JButton component = new JButton("button");

    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);

    System.out.println(inputMap.keys().length);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("button");
    InputMap map = component.getInputMap(JComponent.WHEN_FOCUSED);
    list(map, map.keys());
    list(map, map.allKeys());/* w ww  .j  a v  a  2 s  . c  o m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("button");
    InputMap map = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    list(map, map.keys());
    list(map, map.allKeys());/*from   w w  w .j  av a 2  s.  co m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton();
    InputMap map = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    list(map, map.keys());
    list(map, map.allKeys());/* w  w  w .  ja  va  2 s .c o m*/
}

From source file:org.omegat.gui.shortcuts.PropertiesShortcutsTest.java

/**
 * Test of bindKeyStrokes method, of class PropertiesShortcuts.
 *//*from w  ww  .j  a va  2 s.  c om*/
@Test
public void testBindKeyStrokes_InputMap_ObjectArr() {
    // bind
    InputMap inputMap = new InputMap();
    shotcuts.bindKeyStrokes(inputMap, TEST_SAVE, TEST_CUT, TEST_USER_1);

    // test map size
    long expSize = 3;
    long size = inputMap.size();
    assertEquals(expSize, size);

    // test keys
    KeyStroke[] expResults = new KeyStroke[] { CTRL_S, CTRL_X, CTRL_P };
    KeyStroke[] results = inputMap.keys();
    assertArrayEquals(expResults, results);

    // test entry1 exists
    Object expResult = TEST_SAVE;
    Object result = inputMap.get(CTRL_S);
    assertEquals(expResult, result);

    // test entry2 exists
    expResult = TEST_CUT;
    result = inputMap.get(CTRL_X);
    assertEquals(expResult, result);

    // test entry3 exists
    expResult = TEST_USER_1;
    result = inputMap.get(CTRL_P);
    assertEquals(expResult, result);

    // test remove entry with null shortcut
    inputMap.put(CTRL_D, TEST_DELETE); // put target
    expResult = TEST_DELETE;
    result = inputMap.get(CTRL_D);
    assertEquals(expResult, result); // target exists before remove
    shotcuts.bindKeyStrokes(inputMap, TEST_DELETE); // key to be removed as null
    result = inputMap.get(CTRL_D);
    assertNull(result); // target will be null after removed

    // test map size again
    expSize = 3;
    size = inputMap.size();
    assertEquals(expSize, size);

    // ensure no affect for entry1 after removing
    expResult = TEST_SAVE;
    result = inputMap.get(CTRL_S);
    assertEquals(expResult, result);

    // ensure no affect for entry2 after removing
    expResult = TEST_CUT;
    result = inputMap.get(CTRL_X);
    assertEquals(expResult, result);

    // ensure no affect for entry3 after removing
    expResult = TEST_USER_1;
    result = inputMap.get(CTRL_P);
    assertEquals(expResult, result);
}

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(//from  ww  w  .  j  a v a  2 s .co  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 ww . j a v a 2s.c o  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);

        /*// ww  w .  jav  a2  s.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"));
}