如何在按钮单击java时显式触发按键事件

时间:2014-07-18 05:07:35

标签: java events button keyboard key-events

是否有任何机制可在按钮单击时显式触发按键事件。 当我单击java UI“+”中指定的按钮时,它将显式触发“+”键按下事件。

1 个答案:

答案 0 :(得分:3)

您可以通过多种方式实现这一目标,您使用的方法取决于您的目标。

你可以......

使用java.awt.Robot,这将允许您在本机事件队列中生成键事件。但请记住,此事件将(最终)分配给具有当前键盘焦点的组件...

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);

你可以......

手动调度事件。如果您尝试将事件发送到另一个组件(例如,字段),则可以手动调度该事件

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, '+'));

例如......

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, '+'));
                }
            });
        }

    }

}