使用Java发送密钥组合
作者:互联网
根据之前的链接(How to send keyboard outputs),Java可以模拟使用Robot类按下的键.但是,如何模拟按键的组合?如果我想发送组合“alt-123”这可能使用机器人吗?
解决方法:
简单的答案是肯定的.基本上,您需要将Alt的keyPress / Release包装在其他keyPress / Releases周围
public class TestRobotKeys {
private Robot robot;
public static void main(String[] args) {
new TestRobotKeys();
}
public TestRobotKeys() {
try {
robot = new Robot();
robot.setAutoDelay(250);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_2);
robot.keyRelease(KeyEvent.VK_2);
robot.keyPress(KeyEvent.VK_3);
robot.keyRelease(KeyEvent.VK_4);
robot.keyRelease(KeyEvent.VK_ALT);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
}
标签:awtrobot,java,awt,keypress 来源: https://codeday.me/bug/20190928/1828911.html