Example usage for java.awt.event KeyEvent KeyEvent

List of usage examples for java.awt.event KeyEvent KeyEvent

Introduction

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

Prototype

public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar) 

Source Link

Document

Constructs a KeyEvent object.

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);
        }/*from ww w .j  a va 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, ""));
        }
    }
}

From source file:Main.java

/**
 * Simulate a key press/release event on the component.
 * //from   ww w .j av a2s . co m
 * @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));
}

From source file:jdroidremote.ServerFrame.java

public static void typeCharacter(String letter) {
    System.out.println(letter);//from w w  w  .ja v  a2s .  co m
    Robot robot = null;
    try {
        robot = new Robot();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (Character.isLetter(letter.charAt(0))) {
        try {
            boolean upperCase = Character.isUpperCase(letter.charAt(0));
            String variableName = "VK_" + letter.toUpperCase();

            KeyEvent ke = new KeyEvent(new JTextField(), 0, 0, 0, 0, ' ');
            Class clazz = ke.getClass();
            Field field = clazz.getField(variableName);
            int keyCode = field.getInt(ke);

            //System.out.println(keyCode + " = keyCode");
            robot.delay(80);

            if (upperCase) {
                robot.keyPress(KeyEvent.VK_SHIFT);
            }

            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);

            if (upperCase) {
                robot.keyRelease(KeyEvent.VK_SHIFT);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    } else if (letter.equals(".")) {
        robot.keyPress(KeyEvent.VK_PERIOD); //keyCode 46
        robot.keyRelease(KeyEvent.VK_PERIOD);
    } else if (letter.equals("!")) {
        robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16
        robot.keyPress(KeyEvent.VK_1); //keycode 49
        robot.keyRelease(KeyEvent.VK_1);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } else if (letter.equals(" ")) {
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
    } else if (letter.equals("?")) {
        robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16
        robot.keyPress(KeyEvent.VK_SLASH); //keyCode 47
        robot.keyRelease(KeyEvent.VK_SLASH);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } else if (letter.equals(",")) {
        robot.keyPress(KeyEvent.VK_COMMA);
        robot.keyRelease(KeyEvent.VK_COMMA);
    } else if (letter.equals("@enter")) {
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } else if (letter.equals("@backspace")) {
        robot.keyPress(KeyEvent.VK_BACK_SPACE);
        robot.keyRelease(KeyEvent.VK_BACK_SPACE);
    }
}

From source file:com.AandR.beans.plotting.imagePlotPanel.CanvasPanel.java

/**
 *
 *///from w  w  w. j  a  va  2  s . com
public void actionPerformed(ActionEvent e) {
    JMenuItem item = (JMenuItem) e.getSource();
    int modifiers = item.getAccelerator().getModifiers();
    int keyCode = item.getAccelerator().getKeyCode();
    dispatchEvent(new KeyEvent(this, KeyEvent.KEY_PRESSED, 0, modifiers, keyCode, KeyEvent.CHAR_UNDEFINED));
}

From source file:ch.zhaw.iamp.rct.ui.GrammarWindow.java

private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
    KeyEvent generateEvent = new KeyEvent(this, KeyEvent.VK_F5, System.currentTimeMillis(), 0, KeyEvent.VK_F5,
            '\t');
    grammarTextPaneKeyReleased(generateEvent);
}

From source file:org.eclipse.jubula.rc.swing.driver.RobotAwtImpl.java

/**
 * {@inheritDoc}/*from www .  jav a  2s  . c  o m*/
 */
public void type(Object graphicsComponent, char character) throws RobotException {

    Validate.notNull(graphicsComponent, "The graphic component must not be null"); //$NON-NLS-1$
    try {
        int modifier = 0;
        Component component = (Component) graphicsComponent;
        KeyEvent ke = new KeyEvent(component, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), modifier,
                KeyEvent.VK_UNDEFINED, character);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
        ke = new KeyEvent(component, KeyEvent.KEY_TYPED, System.currentTimeMillis(), modifier,
                KeyEvent.VK_UNDEFINED, character);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
        IRobotEventConfirmer confirmer = m_interceptor
                .intercept(new InterceptorOptions(new long[] { AWTEvent.KEY_EVENT_MASK }));
        ke = new KeyEvent(component, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), modifier,
                KeyEvent.VK_UNDEFINED, character);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
        confirmer.waitToConfirm(component, new DefaultAwtEventMatcher(KeyEvent.KEY_RELEASED));
    } catch (AWTError awte) {
        log.error(awte);
        throw new RobotException(awte);
    } catch (SecurityException se) {
        log.error(se);
        throw new RobotException(se);
    }
}