Java examples for Swing:Event
Returns whether the component that currently has the focus listens for a specified key code, i.e.
import java.awt.*; import javax.swing.*; public class Main{ /**// www . ja v a 2s . c om * Returns whether the component that currently has the focus listens for a * specified key code, i.e. it has an action associated with the key code. * @param keyCode The key code to test. * @return True if the component with the focus listens for the key code, * false otherwise. */ static public boolean currentFocusOwnerListensForKey(int keyCode) { Component focusOwner = KeyboardFocusManager .getCurrentKeyboardFocusManager().getFocusOwner(); if (focusOwner == null) return false; if (focusOwner instanceof JComponent == false) return false; return FocusUtils.componentListensForKey((JComponent) focusOwner, keyCode); } /** * 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; } }