《JAVA模拟键盘鼠标操作》由会员分享,可在线阅读,更多相关《JAVA模拟键盘鼠标操作(3页珍藏版)》请在金锄头文库上搜索。
1、/可以在eclipse下直接运行的示例程序:package key_robot;import import importjava.awt.*;java.awt.event.*;javax.swing.JOptionPane;publicclass key_robot public key_robot() int m, int delay) / 鼠标单击,要双击就连续调用private static void pressMouse(Robot r, r.mousePress(m);r.delay(10); r.mouseRelease(m); r.delay(delay);/ 键盘输入方法 pr
2、ivate static(int iforvoid pressKeys(Robot r, 0; i ks. length; i+) r.keyPress(ksi);r.delay(10); r.keyRelease(ksi); r.delay(delay);int ks, int delay) publicstatic void main(String args)throws Exception final Robot rb = new Robot();new Thread() public void run() rb.delay(2000); / 模拟回车 rb.keyPress(KeyEv
3、ent. VK_ENTER ); rb.keyRelease(KeyEvent. VK_ENTER );.start();JOptionPane. showMessageDialog (null,以下程序自动执行 ,包括本对话框 ,点击运行后请不要移动鼠标和键盘 .n 如果不能正常执行程序,请先关闭输入法);rb.delay(1500);/ 设置开始菜单的大概位置int x = 40;int y = Toolkit. getDefaultToolkit ().getScreenSize(). height - 10; / 鼠标移动到开始菜单 ,rb.mouseMove(x, y);rb.del
4、ay(500);/ 单击三次开始菜单for (int i = 0; i 3; i+)pressMouse (rb, InputEvent. BUTTON1_MASK , 500);rb.delay(1000);/ 运行CMD命令r cmd enterint ks = KeyEvent. VK_R, KeyEvent. VK_C, KeyEvent. VK_M,KeyEvent. VK_D, KeyEvent. VK_ENTER, ;pressKeys(rb, ks, 500);rb.mouseMove(400, 400);rb.delay(500);/ 运行DIR命令dir enterks =
5、 new int KeyEvent. VK_D, KeyEvent. VK_I, KeyEvent. VK_R,KeyEvent. VK_ENTER ;pressKeys(rb, ks, 500);rb.delay(1000);/ 运行CLS命令els enterks = new int KeyEvent. VK_C, KeyEvent. VK_L, KeyEvent. VK_S,KeyEvent. VK_ENTER ;pressKeys(rb, ks, 500);rb.delay(1000);/ 运行EXIT命令exit enterks = new int KeyEvent. VK_E, K
6、eyEvent. VK_X, KeyEvent. VK_I,KeyEvent. VK_T, KeyEvent. VK_ENTER ;pressKeys(rb, ks, 500);rb.delay(1000);/ 右键测试 x = Toolkit. getDefaultToolkit ().getScreenSize(). width - 10; rb.mouseMove(x, y);/ 如果是双键鼠标 ,请改用 InputEvent.BUTTON2_MASK 标 pressMouse (rb, InputEvent. BUTTON3_MASK , 500);/ 显示日期调整对话框 a pressKeys (rb, new int KeyEvent. VK_A , 1000); rb.delay(2000);pressKeys (rb, new int KeyEvent. VK_ESCAPE , 0); rb.delay(1000);new Thread() public void run() rb.delay(1000); / 回车 rb.keyPress(KeyEvent. VK_ENTER ); rb.keyRelease(KeyEvent. VK_ENTER );.start();JOptionPane. showMessageDialog (null, 演示完毕 !);