Może to jest banalnie proste ale jakoś tak już drugi dzień się głowie jak by za symulować KeyEventy związane z wciskaniem i puszczaniem klawiszy. Jak dotychczas kombinowałem troszkę z EventQueue ale jak nic specjalnego z tego nie wyszło :(
0
0
Metoda dispatchEvent
package forum;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.EventHandler;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
* Klasa testująca zdarzenia z klawiatury.
*
* @author Jakub Małek
* @vertion 0.1.0
*/
public class KeyEventTest extends JFrame implements KeyListener, Runnable {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = 9162616502310504629L;
/**
* Przycisk testujący zdarzenie KeyPressed.
*/
private JButton buttonPressKey;
/**
* Przycisk testujący zdarzenie KeyTyped.
*/
private JButton buttonTypeKey;
/**
* Przycisk testujący zdarzenie KeyReleased.
*/
private JButton buttonReleaseKey;
/**
* Konstruktor.
*/
public KeyEventTest() {
super("KeyEventTest");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(120, 160);
this.initComponents();
this.createLayout();
}
/**
* Inicjuje komponenty.
*/
protected void initComponents() {
this.addKeyListener(this);
this.buttonPressKey = new JButton("KeyPressed");
this.buttonPressKey.addActionListener(EventHandler.create(
ActionListener.class, this, "buttonKeyPressed"));
this.buttonTypeKey = new JButton("Key Typed");
this.buttonTypeKey.addActionListener(EventHandler.create(
ActionListener.class, this, "buttonKeyTyped"));
this.buttonReleaseKey = new JButton("Key Released");
this.buttonReleaseKey.addActionListener(EventHandler.create(
ActionListener.class, this, "buttonKeyReleased"));
}
/**
* Tworzy layout.
*/
protected void createLayout() {
this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),
BoxLayout.Y_AXIS));
this.getContentPane().add(this.buttonPressKey);
this.getContentPane().add(this.buttonTypeKey);
this.getContentPane().add(this.buttonReleaseKey);
}
/**
* Syumuluje zdarzenie KEY_PRESSED.
*/
public void buttonKeyPressed() {
this.dispatchEvent(new KeyEvent(this, KeyEvent.KEY_PRESSED,
System.currentTimeMillis(), KeyEvent.VK_ENTER,
KeyEvent.SHIFT_DOWN_MASK, '\n'));
}
/**
* Symuluje zdarzenie KEY_TYPED.
*/
public void buttonKeyTyped() {
this.dispatchEvent(new KeyEvent(this, KeyEvent.KEY_RELEASED,
System.currentTimeMillis(), KeyEvent.ALT_MASK, KeyEvent.VK_A ,'A'));
}
/**
* Symuluje zdarzenie KEY_RELEASED.
*/
public void buttonKeyReleased() {
this.dispatchEvent(new KeyEvent(this, KeyEvent.KEY_RELEASED,
System.currentTimeMillis(), KeyEvent.ALT_MASK, KeyEvent.VK_X ,'x'));
}
/**
* {@inheritDoc}
*/
@Override
public void keyPressed(final KeyEvent e) {
System.out.println("keyPressed " + e);
}
/**
* {@inheritDoc}
*/
@Override
public void keyReleased(final KeyEvent e) {
System.out.println("keyReleased " + e);
}
/**
* {@inheritDoc}
*/
@Override
public void keyTyped(final KeyEvent e) {
System.out.println("keyTyped " + e);
}
/**
* {@inheritDoc}
*/
@Override
public void run() {
this.setVisible(true);
}
/**
* Main.
*
* @param args argumenty
*/
public final static void main(final String[] args) {
final KeyEventTest test = new KeyEventTest();
SwingUtilities.invokeLater(test);
}
}