无为

无为则可为,无为则至深!

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

Java.awt.Robot 类用于控制鼠标和键盘。一旦你得到这种控制,你能够通过你的Java代码做与
鼠标和键盘任何类型的操作.这个类通常用于自动化测试。先面的代码样例将向您展示Robot
类如何处理键盘事件。如果你运行此代码,并打开notepad,您将在notepad中看到HI CAOER。赶快试一试吧。
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class RobotExp {

public static void main(String[] args) {

try {

Robot robot = new Robot();
//定义5秒的延迟以便你打开notepad 哈哈
// Robot 开始写
robot.delay(5000);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);

} catch (AWTException e) {
e.printStackTrace();
}
}
}



凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。

posted on 2007-08-09 14:59 草儿 阅读(2855) 评论(12)  编辑  收藏 所属分类: java

Feedback

# re: 如何在Java中使用Robot类 2007-08-09 17:58 bromon
楼主你好歹也把程序执行一下吧,一看就知道打出来的肯定不是hi budy  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-09 20:29 pass86
hi caoer  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-09 21:22 草儿
哈哈 我后来改了 HI CAOER   回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-10 11:11 sitinspring
这个不错,类似于VBVC的Sendkey,不知有没有对应的AppActivate函数.  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-10 11:27 Swing
其实这个类有时候是很危险的
它可以轻易的对电脑文件进行拖拽删除等  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-10 17:00 HutWang
很有意思.

完善下:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import java.io.IOException;


public class RobotExp {
public static void pressKey(Robot robot, int keyvalue) {
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
}

public static void pressKeyWithShift(Robot robot, int keyvalue) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
robot.keyRelease(KeyEvent.VK_SHIFT);
}

public static void closeApplication(Robot robot) {
// pressKey(robot, KeyEvent.VK_ALT);
// pressKey(robot, KeyEvent.VK_F4);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F4);

//for linux.
// robot.keyPress(KeyEvent.VK_ALT);
// robot.keyPress(KeyEvent.VK_W);
// robot.keyRelease(KeyEvent.VK_ALT);
// robot.keyRelease(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
}

public static void main(String[] args) throws IOException {
try {
Robot robot = new Robot();
Runtime.getRuntime().exec("notepad");

// For linux.
//Runtime.getRuntime().exec("gedit");
//定义5秒的延迟以便你打开notepad 哈哈
// Robot 开始写
robot.delay(3000);

for (int i = 0; i < 100; i++) {
pressKeyWithShift(robot, KeyEvent.VK_H);
pressKey(robot, KeyEvent.VK_I);
pressKey(robot, KeyEvent.VK_SPACE);

//pressKeyWithShift(robot, KeyEvent.VK_H);
pressKeyWithShift(robot, KeyEvent.VK_I);
pressKey(robot, KeyEvent.VK_SPACE);
pressKey(robot, KeyEvent.VK_A);
pressKey(robot, KeyEvent.VK_M);
pressKey(robot, KeyEvent.VK_SPACE);
pressKey(robot, KeyEvent.VK_T);
pressKey(robot, KeyEvent.VK_H);
pressKey(robot, KeyEvent.VK_E);
pressKey(robot, KeyEvent.VK_SPACE);
pressKey(robot, KeyEvent.VK_J);
pressKey(robot, KeyEvent.VK_A);
pressKey(robot, KeyEvent.VK_V);
pressKey(robot, KeyEvent.VK_A);
pressKey(robot, KeyEvent.VK_SPACE);
pressKey(robot, KeyEvent.VK_R);
pressKey(robot, KeyEvent.VK_O);
pressKey(robot, KeyEvent.VK_B);
pressKey(robot, KeyEvent.VK_O);
pressKey(robot, KeyEvent.VK_T);

// VK_ENTER
pressKey(robot, KeyEvent.VK_ENTER);

//pressKey(robot, KeyEvent.);
}

closeApplication(robot);

//robot.keyPress(KeyEvent.VK_SPACE);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-10 17:33 sitinspring
得临时打开程序啊啊,激活已有程序改怎么做?

  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-10 22:09 草儿
你楼上的兄弟已经给了 答案了 哈哈  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-11 08:10 BeanSoft
不能打开组合键, 郁闷. 谁知道如何模拟像 Ctrl + Space 这样的按键组合?  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-11 11:06 草儿
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SPACE );
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SPACE);
这就是组合键,但要包括按下和释放两个动作  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-08-11 11:12 草儿
"Key pressed" and "key released" events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don't generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the getKeyCode method, which returns a virtual key code.

Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as "A", which comes from shift and "a").

  回复  更多评论
  

# re: 如何在Java中使用Robot类 2007-09-15 21:20 黑蝙蝠
@pass86
看来这个和按键精灵的思路一致  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: