Java Swing Key Action pressKey(Component component, int keyCode, int modifier)

Here you can find the source of pressKey(Component component, int keyCode, int modifier)

Description

press Key

License

Apache License

Declaration

public static void pressKey(Component component, int keyCode, int modifier) 

Method Source Code

//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, ""));
            }
        }
    }
}

Related

  1. keyStrokeToString(KeyStroke key)
  2. makeCloseAction(final Window window)
  3. matches(Action action, KeyEvent ke)
  4. parseKeyStroke(String keyStroke)
  5. parseKeyStroke(String keyStroke)
  6. registerKey(JComponent component, final int keyEvent, int modifiers, Action action)
  7. registerKeyAction(JComponent component, int keyCode, Action action)
  8. registerTabKey(Container container)
  9. registerWindowCloseKeys(JRootPane root, Action closeAction)