Example usage for java.awt.event KeyListener keyPressed

List of usage examples for java.awt.event KeyListener keyPressed

Introduction

In this page you can find the example usage for java.awt.event KeyListener keyPressed.

Prototype

public void keyPressed(KeyEvent e);

Source Link

Document

Invoked when a key has been pressed.

Usage

From source file:KeyUtils.java

public static void pressKey(Component component) {
    if (component.getKeyListeners().length > 0) {
        KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, 1, 32, (char) 32);
        for (int i = 0; i < component.getKeyListeners().length; i++) {
            KeyListener keyListener = component.getKeyListeners()[i];
            keyListener.keyPressed(event);
        }// ww  w .  j  av a 2  s .c o  m
    }

    if (JComponent.class.isInstance(component)) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(32, 1);
        final ActionListener actionForKeyStroke = ((JComponent) component).getActionForKeyStroke(keyStroke);
        if (actionForKeyStroke != null) {
            actionForKeyStroke.actionPerformed(new ActionEvent(component, KeyEvent.KEY_PRESSED, ""));
        }
    }
}