Simulate a key held down in Java

前端 未结 3 1511
暗喜
暗喜 2020-12-03 19:50

I\'m looking to simulate the action of holding a keyboard key down for a short period of time in Java. I would expect the following code to hold down the A key for 5 seconds

3条回答
  •  佛祖请我去吃肉
    2020-12-03 20:14

    Just keep pressing?

    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    public class PressAndHold { 
        public static void main( String [] args ) throws Exception { 
            Robot robot = new Robot();
            for( int i = 0 ; i < 10; i++ ) {
                robot.keyPress( KeyEvent.VK_A );
            }
        }
    }
    

    I think the answer provided by edward will do!!

提交回复
热议问题