Using Java's Robot to hold a key down

前端 未结 2 629
谎友^
谎友^ 2020-12-20 06:32

Currently i\'m trying to have java hold down a key like follows:

Robot rob;

rob.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000);
rob.keyRelease(KeyEvent.VK_         


        
相关标签:
2条回答
  • 2020-12-20 06:41

    If I understand your problem, you can't get key repeats to occur when using Robot and keyPress.

    In this case, you may need to produce a "psudo" "long" key press.

    Basically, I tried something like this:

    Robot bot = new Robot();
    bot.setAutoDelay(1);
    
    int duration = 3000;
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < duration) {
        bot.keyPress(KeyEvent.VK_R);
        bot.keyRelease(KeyEvent.VK_R);
    }
    

    Which, rapidly pressed and releases the key over a period of time...

    And I used this to test it...

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.Robot;
    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.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingWorker;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestRobot {
    
        public static void main(String[] args) {
            new TestRobot();
        }
    
        public TestRobot() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JTextArea ta;
    
            public TestPane() {
    
                setLayout(new BorderLayout());
                ta = new JTextArea(20, 20);
                ta.setWrapStyleWord(true);
                ta.setLineWrap(true);
                add(new JScrollPane(ta));
    
                JButton btn = new JButton("Start");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ta.requestFocusInWindow();
                        ta.append("Start\n");
                        SwingWorker worker = new SwingWorker<Object, Object>() {
                            @Override
                            protected Object doInBackground() throws Exception {
    
                                Robot bot = new Robot();
                                bot.setAutoDelay(1);
    
                                int duration = 3000;
                                long start = System.currentTimeMillis();
                                while (System.currentTimeMillis() - start < duration) {
                                    bot.keyPress(KeyEvent.VK_R);
                                    bot.keyRelease(KeyEvent.VK_R);
                                }
                                return null;
    
                            }
    
                            @Override
                            protected void done() {
                                ta.append("\nDone");
                            }
                        };
                        worker.execute();
                    }
                });
                add(btn, BorderLayout.SOUTH);
    
            }
        }
    }
    

    Updated

    With a little testing, I was able to get this to work...

    Robot bot = new Robot();
    bot.setAutoDelay(50);
    
    int duration = 3000;
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < duration) {
        bot.keyPress(KeyEvent.VK_R);
    }
    bot.keyRelease(KeyEvent.VK_R);
    

    Now, if you play around with the autoDelay property, you can adjust the time (in milliseconds) between each event, which may produce a more desirable effect...

    0 讨论(0)
  • 2020-12-20 06:51

    Why not use a for loop?

    for (int i = 0; i < 10; i++)
        rob.keyPress(whatever);
    

    Or, to emulate the hold down effect you want:

    rob.keyPress(whatever);
    Thread.sleep(500);
    for (int i = 0; i < 10; i++) {
        rob.keyPress(whatever);
        Thread.sleep(10);
    }
    

    If you want more "organic" behaviour, just randomize the number of iterations.

    0 讨论(0)
提交回复
热议问题