Here you can find the source of registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)
Parameter | Description |
---|---|
component | Component to register the handler to |
stroke | Keystroke to activate the handler |
listener | ActionListener to handle the key press |
actionCommand | Action command string |
public static void registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)
//package com.java2s; /*/*from w w w. j a va 2s . c o m*/ * Copyright 2006-2015 The MZmine 2 Development Team * * This file is part of MZmine 2. * * MZmine 2 is free software; you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later * version. * * MZmine 2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin * St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { /** * Registers a keyboard handler to a given component * * @param component * Component to register the handler to * @param stroke * Keystroke to activate the handler * @param listener * ActionListener to handle the key press * @param actionCommand * Action command string */ public static void registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand) { component.getInputMap().put(stroke, actionCommand); component.getActionMap().put(actionCommand, new AbstractAction() { /** * */ private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent event) { ActionEvent newEvent = new ActionEvent(event.getSource(), ActionEvent.ACTION_PERFORMED, actionCommand); listener.actionPerformed(newEvent); } }); } }