Java Swing Key Event simulateKeyPressed(Component component, int key)

Here you can find the source of simulateKeyPressed(Component component, int key)

Description

Simulate a key press/release event on the component.

License

Apache License

Parameter

Parameter Description
key KeyEvent.VK_...

Declaration

public static void simulateKeyPressed(Component component, int key) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Component;

import java.awt.event.KeyEvent;

public class Main {
    /**//ww w . j a  va  2 s . c  o  m
     * Simulate a key press/release event on the component.
     * 
     * @param key
     *            KeyEvent.VK_...
     */
    public static void simulateKeyPressed(Component component, int key) {
        component.dispatchEvent(new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, 1, key, KeyEvent.CHAR_UNDEFINED));
        component.dispatchEvent(new KeyEvent(component, KeyEvent.KEY_RELEASED, 0, 2, key, KeyEvent.CHAR_UNDEFINED));
    }
}

Related

  1. pressKey(final int key)
  2. recipientHintSelected(String hintString, TextField toList, boolean shiftKeyPressed)
  3. rejectNonNumericKeys(KeyEvent evt)
  4. retargetKeyEvent(KeyEvent e, Component target)
  5. setKeystroke(JMenuItem m, int key)