Java Swing Key Action sendKeys(JComponent component, int modifiers, int key)

Here you can find the source of sendKeys(JComponent component, int modifiers, int key)

Description

Send a keys with robot

License

Open Source License

Parameter

Parameter Description
component component to send event
modifiers e.g. Ctrl/Shift
key key code to send

Declaration

public static void sendKeys(JComponent component, int modifiers, int key) 

Method Source Code

//package com.java2s;
/*// w ww .  j a v  a  2  s . co m
 * This file is part of the VVIDE project.
 * 
 * Copyright (C) 2011 Pavel Fischer rubbiroid@gmail.com
 * 
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

import java.awt.event.KeyEvent;

import javax.swing.JComponent;

public class Main {
    /**
     * Send a keys with robot
     * 
     * @param component
     *        component to send event
     * @param modifiers
     *        e.g. Ctrl/Shift
     * @param key
     *        key code to send
     */
    public static void sendKeys(JComponent component, int modifiers, int key) {
        KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, modifiers, key, KeyEvent.CHAR_UNDEFINED,
                KeyEvent.KEY_LOCATION_STANDARD);
        component.requestFocusInWindow();
        component.dispatchEvent(event);
    }
}

Related

  1. registerKeyAction(JComponent component, int keyCode, Action action)
  2. registerTabKey(Container container)
  3. registerWindowCloseKeys(JRootPane root, Action closeAction)
  4. removeTabbedPaneFocusTraversalKeyBindings( JComponent c)
  5. replaceAction(InputMap map, char c)
  6. setActionID(final Action a, final String id)
  7. setDefaultOkCancelKeyStrokes(final JRootPane rootPane, final Action okAction, final Action cancelAction)
  8. setEscapeKeyboardAction(final Window window, JComponent pane)
  9. setKeyEvent(JComponent widget, int keyEvent, Runnable action)