Robot and Clipboard
2010-08-27 11:31
239 查看
import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.KeyEvent; import java.io.IOException; public class ClipManager { private static Robot robot = null; public Robot getRobot(){ if(robot==null){ try { robot=new Robot(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return robot; } protected void pasteStringFromClipboard(String clipContent){ Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipText = new StringSelection(clipContent); clipboard.setContents(clipText, null); } public String getClipboardContents() { String result = ""; //odd: the Object param of getContents is not currently used Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable contents = clipboard.getContents(null); boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor); if ( hasTransferableText ) { try { // DataFlavor[] dfs=contents.getTransferDataFlavors(); // for(int i=0;i<dfs.length;i++){ // System.out.println(dfs[i].toString()); // } // StringReader sr=(StringReader)contents.getTransferData(DataFlavor.plainTextFlavor); // int ch; // while((ch=sr.read())!=-1){ // result+=(char)ch; // System.out.print((char)ch); // } result = (String)contents.getTransferData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException ex){ //highly unlikely since we are using a standard DataFlavor System.out.println(ex); ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } return result; } public static void main(String[] args){ try { Process p = Runtime.getRuntime().exec("java -v"); int exitCode=p.waitFor(); if(exitCode!=0){ System.out.println("Process failed!"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ClipManager cm=new ClipManager(); cm.pasteStringFromClipboard("aaa"); cm.getRobot().keyPress(KeyEvent.VK_ALT); cm.getRobot().keyPress(KeyEvent.VK_TAB); cm.getRobot().keyRelease(KeyEvent.VK_ALT); cm.getRobot().keyRelease(KeyEvent.VK_TAB); cm.getRobot().keyPress(KeyEvent.VK_CONTROL); cm.getRobot().keyPress(KeyEvent.VK_C); cm.getRobot().keyRelease(KeyEvent.VK_CONTROL); cm.getRobot().keyRelease(KeyEvent.VK_C); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // cm.pasteStringFromClipboard("bbb"); System.out.println(cm.getClipboardContents()); } }
Point here is CPU is too fast so that is why you
1) put p.waitfor() to wait the process finish running
2) Put thread.sleep(5000) to wait for the Ctrl+C to put the copied contents to clipboard.
Note: Honestly 5000 is too long 200 will do the trick but I didn't modify the code because I'm lazy.
相关文章推荐
- What is difference between Vim's clipboard “unnamed” and “unnamedplus” settings?
- [CF 294D]Shaass and Painter Robot解题报告
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
- 【codeforces 752C】Santa Claus and Robot
- CF 322C - Ciel and Robot 计算周期
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
- CF#354 A. Vasya and Robot(思维题)
- C. Vasya and Robot
- CodeForces 748C Santa Claus and Robot (思维)
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- how to sikuli and robot framework
- 『HTML5梦幻之旅』 - 仿Qt演示样例Drag and Drop Robot(换装机器人)
- Codeforces Round #389 Div.2 C. Santa Claus and Robot
- copy & paste between vim and system clipboard, vim paste in insert mode
- Robot RIDE and Eclipse Plugin
- Robotuim学习FAQ-问题4:The connection to adb is down, and a severe error has occured(Android模拟器端口被占用
- A robot with a view—how drones and machines can navigate on their own [video]
- Vasya and Robot
- CodeForces 321 A - Ciel and Robot
- Badminton-playing robot and energy efficiency software