java的Robot类本来是用作java窗口应用程序自动测试的,我用来实现了校园网自动登录功能
package com.sjc.toy;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class TestClient {
public static void main(String[] args) {
new TestClient().doThis();
}
public void doThis(){
T t = new T();
t.start();
}
class T extends Thread{
@Override
public void run(){
try {
Robot robot = new Robot();
while(true) {
Process p = Runtime.getRuntime().exec("H:\\ChinaNetSn\\bin\\NetKeeper.exe");
Thread.sleep(1000);
//要7个Tab键才能使焦点位于登录按钮上
for(int i=0; i<7; i++) {
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(120000);
p.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
代码质量差,还望各位大牛们多多指教