1.能夠通過(guò)robot對(duì)象操作鍵盤(pán)上的按鍵完成復(fù)制粘貼,切換焦點(diǎn)和回車(chē)等常用操作。
2.要使用Robot類(lèi)首先要導(dǎo)入Robot類(lèi)以及所需要的類(lèi)
import java.awt.AWTException;//所在平臺(tái)不支持Robot類(lèi)的異常類(lèi)
import java.awt.Robot;//Robot類(lèi)
------------------------------------------------------------
(1)封裝的粘貼函數(shù)
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);
}