Java:使用Robot类模拟键盘, 以Alt码方式输出汉字
文章目录
java.awt.Robot类
Java提供java.awt.Robot类来模拟操作键盘和鼠标, 下面是一个简单的demo
public static void keyPressByInt(Robot r,int key, int time){
r.keyPress(key);
r.keyRelease(key);
if (time > 0) {
r.delay(time);
}
}
public static void main(String[] args) throws AWTException, CharacterCodingException {
Robot robot = new Robot();
keyPressByInt(robot, KeyEvent.VK_H, 5);
keyPressByInt(robot, KeyEvent.VK_E, 5);
keyPressByInt(robot, KeyEvent.VK_L, 5);
keyPressByInt(robot, KeyEvent.VK_L, 5);
keyPressByInt(robot, KeyEvent.VK_O