Reputation: 608
Is there any mechanism to trigger key press event explicitly when button click. When I click a button named in java UI "+" it will explicitly trigger "+" key press event.
Upvotes: 1
Views: 3428
Reputation: 347244
There are a number of ways you might achieve this, which one you use will depend on what you are trying to do.
Use java.awt.Robot
, which will allow you to generate a key event within the native event queue. Remember though, this event will (eventually) be dispatched to the component with the current keyboard focus...
Robot bot = new Robot();
bot.setAutoDelay(250);
bot.keyPress(KeyEvent.SHIFT_KEY);
bot.keyPress(KeyEvent.EQUALS); // Shift + "=" = +
bot.keyRelease(KeyEvent.EQUALS);
bot.keyRelease(KeyEvent.SHIFT_KEY);
Dispatch the event manually. If you're trying to send the event to another component, a field for example, you could dispatch the event manually
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_TYPED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
For example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ButtonTrigger {
public static void main(String[] args) {
new ButtonTrigger();
}
public ButtonTrigger() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField field;
private JButton button;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
field = new JTextField(10);
button = new JButton("+");
add(field, gbc);
add(button, gbc);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_TYPED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_UNDEFINED, '+'));
field.dispatchEvent(new KeyEvent(field, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), KeyEvent.SHIFT_DOWN_MASK, KeyEvent.VK_EQUALS, '+'));
}
});
}
}
}
Upvotes: 3