Using Java awt. Robot simulation backspace key
•
Java
Using Java awt. There seems to be a problem with robot's simulated backspace key
This thread seems to confirm this, but it does not propose a solution
This works:
Robot rob = new Robot(); rob.keyPress(KeyEvent.VK_A); rob.keyRelease(KeyEvent.VK_A);
This is not:
Robot rob = new Robot(); rob.keyPress(KeyEvent.VK_BACK_SPACE); rob.keyRelease(KeyEvent.VK_BACK_SPACE);
Any ideas?
Solution
It seems to work in this test
Appendix: about the quoted article, "don't rely on the values of VK constants except those keys defined in the Java language (VK enter, VK back space and VK tab). Sun reserves the right to change these values as needed. Adapt to the wider keyboard in the future." – java.awt.event.keyevent
public class RobotTest { public static void main(String[] args) { EventQueue.invokelater(new Runnable() { public void run() { new Robottest().create(); } }); } private void create() { JFrame f = new JFrame(); f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); f.setLayout(new FlowLayout()); f.add(new JTextField(8)); final JButton b = new JButton(); f.getRootPane().setDefaultButton(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { b.setText("@" + e.getWhen()); } }); f.add(b); f.setSize(256,128); f.setVisible(true); dotest(); } private void dotest() { try { Robot r = new Robot(); int[] keys = { KeyEvent.VK_T,KeyEvent.VK_E,KeyEvent.VK_S,KeyEvent.VK_T,KeyEvent.VK_Z,KeyEvent.VK_BACK_SPACE,KeyEvent.VK_ENTER }; for (int code : keys) { r.keyPress(code); r.keyRelease(code); } } catch (AWTException ex) { ex.printStackTrace(System.err); } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码