The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > Keystrokes and Input Maps  [6 examples]

e862. Finding a Key Binding in a Component

This example searches all of a component's inputmaps and keymaps (if the component is a text component) for a particular keystroke.
    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;
    }

 Related Examples
e858. Creating a KeyStroke and Binding It to an Action
e859. Converting a KeyStroke to a String
e860. Listing the Key Bindings in a Component
e861. Sharing an InputMap or an ActionMap Between Two Components
e863. Adding an InputMap to a Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.