1. 程式人生 > >用java控制window視窗的切換

用java控制window視窗的切換

package pack;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyTimer extends TimerTask{

	@Override
    public void run(){
        System.out.println("5秒一次");
        try {
            new Robot().keyPress(KeyEvent.VK_ALT);
            new Robot().keyPress(KeyEvent.VK_TAB);
            Thread.sleep(1000);
            new Robot().keyPress(KeyEvent.VK_ENTER);
            
        } catch (AWTException ex) {
            Logger.getLogger(MyTimer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	public static void main(String[] args){
		
        System.out.println("5秒一次");
        try {
        	//find the first window
            new Robot().keyPress(KeyEvent.VK_ALT);
            new Robot().keyPress(KeyEvent.VK_TAB);
            Thread.sleep(100);
            new Robot().keyPress(KeyEvent.VK_ENTER);
            Thread.sleep(100);
            
            //find the second window
            new Robot().keyPress(KeyEvent.VK_ALT);
            new Robot().keyPress(KeyEvent.VK_TAB);
            new Robot().keyPress(KeyEvent.VK_TAB);
            Thread.sleep(100);
            new Robot().keyPress(KeyEvent.VK_ENTER);
            
        } catch (AWTException ex) {
            Logger.getLogger(MyTimer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //window change
		new Timer().schedule(new MyTimer(), 1000, 5000);
	}

}