1.能够通过robot对象操作键盘上的按键完成复制粘贴,切换焦点和回车等常用操作。
2.要使用Robot类首先要导入Robot类以及所需要的类
import java.awt.AWTException;//所在平台不支持Robot类的异常类
import java.awt.Robot;//Robot类
------------------------------------------------------------
(1)封装的粘贴函数
public void setAndctrlVClipboardData (String string){?
StringSelection stringSelection=new StringSelection(string);
Tookit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection,null);
Robot robot=null;
try{robot =new Robot();?}
catch(AWTException el){el.printStackTrace();}
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelaese(KeyEvent.VK_CONTROL);
}
(2)封装好的按下tab键
public void pressTabKey(){
Robot robot=null;
try{robot =new Robot();?}
catch(AWTException el){el.printStackTrace();}
robot.keyPress(keyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
(3)封装好的按下enter键
public void pressEnterKey(){
Robot robot=null;
try{robot =new Robot();?}
catch(AWTException el){el.printStackTrace();}
robot.keyPress(keyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}