01: package org.uispec4j.utils;
02:
03: import org.uispec4j.Key;
04:
05: import javax.swing.*;
06: import java.awt.*;
07: import java.awt.event.ActionEvent;
08: import java.awt.event.ActionListener;
09: import java.awt.event.KeyEvent;
10: import java.awt.event.KeyListener;
11:
12: public class KeyUtils {
13: public static void pressKey(Component component, Key key) {
14: int keyCode = key.getCode();
15: int modifier = key.getModifier().getCode();
16: if (component.getKeyListeners().length > 0) {
17: KeyEvent event = new KeyEvent(component,
18: KeyEvent.KEY_PRESSED, 0, modifier, keyCode,
19: (char) keyCode);
20: for (int i = 0; i < component.getKeyListeners().length; i++) {
21: KeyListener keyListener = component.getKeyListeners()[i];
22: keyListener.keyPressed(event);
23: }
24: }
25:
26: if (JComponent.class.isInstance(component)) {
27: KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode,
28: modifier);
29: final ActionListener actionForKeyStroke = ((JComponent) component)
30: .getActionForKeyStroke(keyStroke);
31: if (actionForKeyStroke != null) {
32: actionForKeyStroke.actionPerformed(new ActionEvent(
33: component, KeyEvent.KEY_PRESSED, ""));
34: }
35: }
36: }
37: }
|