java robot

3186 ワード

..
 
package t;

import java.awt.Robot;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 *   
 * https://blog.csdn.net/zone_four/article/details/85783207
 * @author Administrator
 *
 */
public class Util {
	  /**
     *          (  )
     */
    public  String getClipboardString() {
        //        
        Clipboard clipboard =java.awt.Toolkit. getDefaultToolkit().getSystemClipboard();

        //          
        Transferable trans = clipboard.getContents(null);

        if (trans != null) {
            //                
            if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    //            
                    String text = (String) trans.getTransferData(DataFlavor.stringFlavor);
                    return text;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
    /**
     *          (  )
     */
    public static void setClipboardString(String text) {
        //        
        Clipboard clipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
        //       
        Transferable trans = new StringSelection(text);
        //              
        clipboard.setContents(trans, null);
    }
    String lastOne="";
	public void dothing(){
		try{
			 Robot robot = new Robot();
			 //   
	         robot.keyPress(KeyEvent.VK_WINDOWS);
	         robot.keyPress(KeyEvent.VK_D);
	         robot.keyRelease(KeyEvent.VK_D);
	         robot.keyRelease(KeyEvent.VK_WINDOWS);
	         
			 	//  
	         	robot.delay(400);
		       //          
		         robot.mouseMove(152,754);	
		       //         ,    
		 		robot.mousePress(KeyEvent.BUTTON1_MASK);
		 		robot.mouseRelease(KeyEvent.BUTTON1_MASK);
		 		
		 		 robot.delay(200);
		 		 
		 		robot.keyPress(KeyEvent.VK_CONTROL);
		 		robot.keyPress(KeyEvent.VK_1);
		 		
		 		robot.keyRelease(KeyEvent.VK_1);
				robot.keyRelease(KeyEvent.VK_CONTROL);

//		 		
			       //          
		 		 
		 		 for(int i=0;i<60;i++){

			 		robot.delay(1500);
			 		
			 		
			 		int startx=1092;
			 		int starty=293;
			 		
		 			robot.mouseMove(startx,starty+130*0);	
//		 			
//		 			robot.mouseMove(993*(i+122),520);	
		 			//   
//robot.delay(22200);
			 		//       
		 			robot.mousePress(KeyEvent.BUTTON1_MASK);
			 		robot.mouseRelease(KeyEvent.BUTTON1_MASK);
			 		robot.delay(100);
//			 		//          
//			 		robot.keyPress(KeyEvent.VK_CONTROL);
//			 		robot.keyPress(KeyEvent.VK_SHIFT);
//			 		robot.keyPress(KeyEvent.VK_C);
//			 		robot.keyRelease(KeyEvent.VK_CONTROL);
//					robot.keyRelease(KeyEvent.VK_SHIFT);
//					robot.keyRelease(KeyEvent.VK_C);
//					robot.delay(500);
					String str=getClipboardString();
					System.out.println("1start:"+startx+" y:"+starty+"\t content:"+getClipboardString());
//					if(str.contains("