Robot and Clipboard

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

 

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.

你可能感兴趣的:(Script,Java,string,null,import,object,class,java)