Here you can find the source of registerKeyAction(JComponent component, int keyCode, Action action)
Parameter | Description |
---|---|
component | The component against which the key action is registered. |
keyCode | The key code to register, as specified in the KeyEvent class's VK constants. |
action | The action to perform when the key stroke is received. |
public static void registerKeyAction(JComponent component, int keyCode, Action action)
//package com.java2s; //License from project: Apache License import javax.swing.*; public class Main { /**//from w ww . j a v a2s . c o m * Registers an action against a key stroke for a particular component. Must * be invoked from EDT. * * @param component The component against which the key action is * registered. * @param when The condition when this action is active (see * JComponent.WHEN...) * @param key The key stroke to register. * @param action The action to perform when the key stroke is received. */ public static void registerKeyAction(JComponent component, int when, KeyStroke key, Action action) { component.getInputMap(when).put(key, key); component.getActionMap().put(key, action); } /** * A simplified version of another method of the same name for registering a * handler for certain key events. Must be invoked from EDT. * * @param component The component against which the key action is * registered. * @param keyCode The key code to register, as specified in the KeyEvent * class's VK constants. * @param action The action to perform when the key stroke is received. */ public static void registerKeyAction(JComponent component, int keyCode, Action action) { registerKeyAction(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KeyStroke.getKeyStroke(keyCode, 0), action); } /** * A simplified version of another method of the same name for registering a * handler for certain key events. Must be invoked from EDT. * * @param component The component against which the key action is * registered. * @param keyCode The key code to register, as specified in the KeyEvent * class's VK constants. * @param modifiers Combination of zero or more modifiers from * InputEvent.<i>XXX</i>_DOWN_MASK * @param action The action to perform when the key stroke is received. */ public static void registerKeyAction(JComponent component, int keyCode, int modifiers, Action action) { registerKeyAction(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KeyStroke.getKeyStroke(keyCode, modifiers), action); } }