使用robot封装一个模拟键盘复制粘贴并按下回车的方法

    /**
     * 复制数据到剪切板并粘贴出来并按下回车
     * @param writeMe 需要粘贴的地址
     * @throws java.awt.AWTException
     */
    public void useSysClipboard(String writeMe) throws AWTException { 
        Sleeper.sleepTight(500);
        Robot robot=new Robot();
        System.out.println(writeMe);
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();  
        clip.setContents(new StringSelection(""), null);
        Transferable tText = new StringSelection(writeMe);  
        clip.setContents(tText, null);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        Sleeper.sleepTight(500);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        Sleeper.sleepTight(500);
        robot.keyPress(KeyEvent.VK_ENTER);
        Sleeper.sleepTight(500);
    }

 

posted @ 2016-09-12 20:50  xxyBlogs  阅读(1587)  评论(0编辑  收藏  举报