Here you can find the source of pressKey(Component component, int keyCode, int modifier)
public static void pressKey(Component component, int keyCode, int modifier)
//package com.java2s; //License from project: Apache License import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main { public static void pressKey(Component component, int keyCode, int modifier) { if (component.getKeyListeners().length > 0) { KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, modifier, keyCode, (char) keyCode); for (int i = 0; i < component.getKeyListeners().length; i++) { KeyListener keyListener = component.getKeyListeners()[i]; keyListener.keyPressed(event); }//from w w w . j a v a2s . c o m } if (JComponent.class.isInstance(component)) { KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifier); ActionListener actionForKeyStroke = ((JComponent) component).getActionForKeyStroke(keyStroke); if (actionForKeyStroke != null) { actionForKeyStroke.actionPerformed(new ActionEvent(component, KeyEvent.KEY_PRESSED, "")); } } } }