Java Swing KeyStroke componentListensForKey(JComponent component, int keyCode)

Here you can find the source of componentListensForKey(JComponent component, int keyCode)

Description

Returns whether a component listens for a key code, i.e.

License

Open Source License

Parameter

Parameter Description
component The component to examine.
keyCode The key code to test.

Return

True if the component listens for the key code, false otherwise.

Declaration

static public boolean componentListensForKey(JComponent component, int keyCode) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

public class Main {
    /**//w  ww .  j  ava  2s. co m
     * Returns whether a component listens for a key code, i.e. it has an 
     * action associated with the key code.
     * @param component The component to examine.
     * @param keyCode The key code to test.
     * @return True if the component listens for the key code, false otherwise.
     */
    static public boolean componentListensForKey(JComponent component, int keyCode) {
        KeyStroke[] keyStrokes = component.getInputMap().allKeys();
        if (keyStrokes == null)
            return false;
        for (int i = keyStrokes.length - 1; i >= 0; i--) {
            if (keyCode == keyStrokes[i].getKeyCode()) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. bindKeyToAction(int keyCode, int modifiers, Action action, JComponent component, int condition)
  2. bindKeyToAction(JComponent c, KeyStroke key, Action a)
  3. clearActionBinding(final JComponent component, final KeyStroke keyStroke, final int condition)
  4. clearKeyStroke(JComponent component)
  5. closeOnKeyStroke(JRootPane rootPane, KeyStroke keyStroke)
  6. configAction(Action action, String text, Icon icon, KeyStroke keyStroke)
  7. convertShortcutMask(KeyStroke ks, int shortcutMask)
  8. createItem(String name, Integer mnem, KeyStroke accel)
  9. createKeyMask(final int aKeyStroke, final int... aMasks)