public static void pressKey(Component component, Key key) { dispatchEvent(KEY_PRESSED, key, key.getCode(), component); if (key.isPrintable() && key.getChar() != KeyEvent.CHAR_UNDEFINED) { dispatchEvent(KEY_TYPED, key, KeyEvent.VK_UNDEFINED, component); } if (JTextComponent.class.isInstance(component) && key.isPrintable()) { moveCaretToEndOfDocument((JTextComponent) component); } }
private static void dispatchEvent(int eventCode, Key key, int keyCode, Component component) { int modifier = key.getModifier().getCode(); KeyEvent event = new KeyEvent(component, eventCode, 0, modifier, keyCode, key.getChar()); for (KeyListener listener : component.getKeyListeners()) { switch (eventCode) { case KEY_PRESSED: listener.keyPressed(event); break; case KEY_TYPED: listener.keyTyped(event); break; case KEY_RELEASED: listener.keyReleased(event); break; } } SwingUtilities.processKeyBindings(event); }
public static void releaseKey(Component component, Key key) { dispatchEvent(KEY_RELEASED, key, key.getCode(), component); }