![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e858. Creating a KeyStroke and Binding It to an ActionThis example creates a number of keystrokes and adds them to the input map of a component. When a keystroke is added to an input map, an action name must be supplied. This action is invoked when the keystroke is pressed while the component has the focus.// Create some keystrokes and bind them all to the same action component.getInputMap().put(KeyStroke.getKeyStroke("F2"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke("control A"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke("shift F2"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke('('), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke("button3 F"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke("typed x"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke("released DELETE"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke("shift UP"), "actionName"); // Add the action to the component component.getActionMap().put("actionName", new AbstractAction("actionName") { public void actionPerformed(ActionEvent evt) { process(evt); } } );
e860. Listing the Key Bindings in a Component e861. Sharing an InputMap or an ActionMap Between Two Components e862. Finding a Key Binding in a Component e863. Adding an InputMap to a Component © 2002 Addison-Wesley. |