FindKeyBind

FindResult r = find(KeyStroke.getKeyStroke("ctrl pressed C"), component);
r = find(KeyStroke.getKeyStroke("ctrl released C"), component);
r = find(KeyStroke.getKeyStroke("C"), component);
r = find(KeyStroke.getKeyStroke("typed C"), component);
r = find(KeyStroke.getKeyStroke(new Character('u0002'), 0), component);

// The results of a find are returned in a FindResult object
static class FindResult {
    // Non-null if the keystroke is in an inputmap
    InputMap inputMap;

    // Non-null if the keystroke is in an keymap or default action
    Keymap keymap;

    // Non-null if the keystroke is in a default action
    // The keymap field holds the keymap containing the default action
    Action defaultAction;

    // If true, the keystroke is in the component's inputMap or keymap
    // and not in one of the inputMap's or keymap's parent.
    boolean isLocal;

    public String toString() {
        StringBuffer b = new StringBuffer();

        b.append("inputmap="+inputMap+",keymap="+keymap
                 +",defaultAction="+defaultAction+",isLocal="+isLocal);
        return b.toString();
    }
}

// Returns null if not found
public static FindResult find(KeyStroke k, JComponent c) {
    FindResult result;

    result = find(k, c.getInputMap(JComponent.WHEN_FOCUSED));
    if (result != null) {
        return result;
    }
    result = find(k, c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
    if (result != null) {
        return result;
    }
    result = find(k, c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
    if (result != null) {
        return result;
    }


    // Check keymaps
    if (c instanceof JTextComponent) {
        JTextComponent tc = (JTextComponent)c;
        result = new FindResult();

        // Check local keymap
        Keymap kmap = tc.getKeymap();
        if (kmap.isLocallyDefined(k)) {
            result.keymap = kmap;
            result.isLocal = true;
            return result;
        }

        // Check parent keymaps
        kmap = kmap.getResolveParent();
        while (kmap != null) {
            if (kmap.isLocallyDefined(k)) {
                result.keymap = kmap;
                return result;
            }
            kmap = kmap.getResolveParent();
        }

        // Look for default action
        if (k.getKeyEventType() == KeyEvent.KEY_TYPED) {
            // Check local keymap
            kmap = tc.getKeymap();
            if (kmap.getDefaultAction() != null) {
                result.keymap = kmap;
                result.defaultAction = kmap.getDefaultAction();
                result.isLocal = true;
                return result;
            }

            // Check parent keymaps
            kmap = kmap.getResolveParent();
            while (kmap != null) {
                if (kmap.getDefaultAction() != null) {
                    result.keymap = kmap;
                    result.defaultAction = kmap.getDefaultAction();
                    return result;
                }
                kmap = kmap.getResolveParent();
            }
        }
    }
    return null;
}

public static FindResult find(KeyStroke k, InputMap map) {
    // Check local inputmap
    KeyStroke[] keys = map.keys();
    for (int i=0; keys != null && i<keys.length; i++) {
        if (k.equals(keys[i])) {
            FindResult result = new FindResult();
            result.inputMap = map;
            result.isLocal = true;
            return result;
        }
    }

    // Check parent inputmap
    map = map.getParent();
    while (map != null) {
        keys = map.keys();
        for (int i=0; keys != null && i<keys.length; i++) {
            if (k.equals(keys[i])) {
                FindResult result = new FindResult();
                result.inputMap = map;
                return result;
            }
        }
        map = map.getParent();
    }
    return null;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.