简单的程序,自动鼠标和键盘运动

时间:2011-04-30 20:00:03

标签: google-chrome login passwords

背景:

我每周在线检查我的成绩以便上学。然后我使用print-screen和ms paint来保存我的成绩的网站图像。 (我知道这非常耗时且效率低,但我的计算机与我的打印机存在连接问题,并且无法识别任何PDF制造商)

可能的解决方案:

自动编写一些程序或其他内容:

  1. 打开浏览器
  2. 访问网站
  3. 登录
  4. 打开每个主题
  5. 创建一个图像(idc用于执行此操作的过程)
  6. 谢谢E'rybody

4 个答案:

答案 0 :(得分:2)

不要使用浏览器。以perl或类似的方式发送Http请求。

您只需解析HTML而不是抓取屏幕截图。

或者要求学校通过电子邮件或 bloddy 纸张提供您的成绩?

答案 1 :(得分:1)

我个人会从命令行使用curl然后解析你得到的html:

curl http://website.com/grades

但是,如果你需要通过网络浏览器(他们可能有一些时髦的javascript),那么有一个名为selenium的框架可能是可用的:http://seleniumhq.org/

它通常用于测试,但可用于您的场景。它也有许多不同语言的客户。

答案 2 :(得分:0)

您可以在java.awt中使用Robot类编写一个简单的java程序。这很简单。您可以自动化所有内容,因为计算机前面有人。可能需要一点努力。但相信我,你可以让它完美地解决你的问题。

如果您对此有任何疑问,请随时与我联系。我会帮忙的。

答案 3 :(得分:0)

试试这个。这有点长。但它会告诉你Robot类的功能。您可以通过在'go()'方法中调用它们来尝试所有方法。如果您稍微耐心一点,那么您将能够像操作人员一样自动执行鼠标和键盘操作。

本课程是为更高级的功能而编写的(当然,只是为了好玩)。所以,你不需要那些先进的方法。你可以通过他们的名字来理解。

请注意,我发布了由我撰写的原始课程(sumudu lansakara或Codenamed_SC)。我没有根据您的要求对其进行修改或使其看起来很简单。你可以自己做那个部分。 (但我认为完整代码现在或以后都会有所帮助。)

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MyRobot {

Robot MYROBOT;
Color SCREEN[][];
int SCREEN_X;
int SCREEN_Y;
int WINDOW_X;
int WINDOW_Y;
int TYPE_DELAY = 0;
int MOUSE_DELAY = 0;
int SKILL = 0;
int X, Y;
int lastX, lastY;
int savedX, savedY;
int RED, GREEN, BLUE;
Color PIX_CLOSE_ACTIVE_OFF_MOUSE;
Color PIX_CLOSE_ACTIVE_ON_MOUSE;
HashMap<Character, Integer> KEYMAP;
ArrayList<Integer> randKeys;
GraphicsEnvironment grEnv;
GraphicsDevice grDevice;
JPanel drawPanel;
BufferedImage bufImg;
Rectangle rect;

public static void main(String[] args) {
    new MyRobot().go();
}

void go() {
    initialize();        
    gotoxy(100, 100);
}

void initialize() {
    grEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    grDevice = grEnv.getDefaultScreenDevice();
    updateScreenInfo();
    setKeyMap();
    try {
        MYROBOT = new Robot();
    } catch (Exception e) {
        JOptionPane.showOptionDialog(null, "Can't build the robot!", "Error", -1, 1, null, null, this);
        System.exit(1);
    }
    X = SCREEN_X / 2;
    Y = SCREEN_Y / 2;
    //MYROBOT.mouseMove(X, Y);
    PIX_CLOSE_ACTIVE_OFF_MOUSE = new Color(184, 67, 44);
    PIX_CLOSE_ACTIVE_ON_MOUSE = new Color(210, 35, 2);
}

void updateScreenInfo() {
    SCREEN_X = grDevice.getDisplayMode().getWidth();
    SCREEN_Y = grDevice.getDisplayMode().getHeight();
    WINDOW_X = grEnv.getMaximumWindowBounds().width;
    WINDOW_Y = grEnv.getMaximumWindowBounds().height;
}

void setKeyMap() {
    KEYMAP = new HashMap<Character, Integer>();
    KEYMAP.put('q', 81);
    KEYMAP.put('w', 87);
    KEYMAP.put('e', 69);
    KEYMAP.put('r', 82);
    KEYMAP.put('t', 84);
    KEYMAP.put('y', 89);
    KEYMAP.put('u', 85);
    KEYMAP.put('i', 73);
    KEYMAP.put('o', 79);
    KEYMAP.put('p', 80);
    KEYMAP.put('a', 65);
    KEYMAP.put('s', 83);
    KEYMAP.put('d', 68);
    KEYMAP.put('f', 70);
    KEYMAP.put('g', 71);
    KEYMAP.put('h', 72);
    KEYMAP.put('j', 74);
    KEYMAP.put('k', 75);
    KEYMAP.put('l', 76);
    KEYMAP.put('z', 90);
    KEYMAP.put('x', 88);
    KEYMAP.put('c', 67);
    KEYMAP.put('v', 86);
    KEYMAP.put('b', 66);
    KEYMAP.put('n', 78);
    KEYMAP.put('m', 77);
    KEYMAP.put('1', 49);
    KEYMAP.put('2', 50);
    KEYMAP.put('3', 51);
    KEYMAP.put('4', 52);
    KEYMAP.put('5', 53);
    KEYMAP.put('6', 54);
    KEYMAP.put('7', 55);
    KEYMAP.put('8', 56);
    KEYMAP.put('9', 57);
    KEYMAP.put('0', 48);
    KEYMAP.put(' ', 32);

    randKeys = new ArrayList<Integer>();
    for (int j = 44; j < 94; j++) {
        randKeys.add(j);
    }
    randKeys.remove(randKeys.indexOf(58));
    randKeys.remove(randKeys.indexOf(60));
    randKeys.remove(randKeys.indexOf(62));
    randKeys.remove(randKeys.indexOf(63));
    randKeys.remove(randKeys.indexOf(64));
}

void typeHuman(String letters) {
    MYROBOT.setAutoDelay(TYPE_DELAY);
    char[] letter = letters.toCharArray();
    for (int i = 0; i < letter.length; i++) {
        System.out.print(letter[i]);
        try {
            if ((int) (Math.random() * SKILL) == (int) (Math.random() * SKILL) / 2) {
                int limit = (int) ((Math.random() * 3) + 1);
                for (int j = 0; j < limit; j++) {
                    int k = (int) (Math.random() * (randKeys.size() - 1));
                    manuPress(randKeys.get(k));
                }
                sleep(200);
                for (int j = 0; j < limit; j++) {
                    backspace();
                }
            }
            if ((Character.isLowerCase(letter[i])) || (Character.isDigit(letter[i]))) {
                MYROBOT.keyPress(KEYMAP.get(letter[i]));
                MYROBOT.keyRelease(KEYMAP.get(letter[i]));
            } else {
                letter[i] = Character.toLowerCase(letter[i]);
                MYROBOT.keyPress(16);
                MYROBOT.keyPress(KEYMAP.get(letter[i]));
                MYROBOT.keyRelease(KEYMAP.get(letter[i]));
                MYROBOT.keyRelease(16);
            }
        } catch (Exception e) {
            switch (letter[i]) {
                case '!': {
                    manuPressSHIFTED(49);
                    break;
                }
                case '@': {
                    manuPressSHIFTED(50);
                    break;
                }
                case '#': {
                    manuPressSHIFTED(51);
                    break;
                }
                case '$': {
                    manuPressSHIFTED(52);
                    break;
                }
                case '%': {
                    manuPressSHIFTED(53);
                    break;
                }
                case '^': {
                    manuPressSHIFTED(54);
                    break;
                }
                case '&': {
                    manuPressSHIFTED(55);
                    break;
                }
                case '*': {
                    manuPressSHIFTED(56);
                    break;
                }
                case '(': {
                    manuPressSHIFTED(57);
                    break;
                }
                case ')': {
                    manuPressSHIFTED(48);
                    break;
                }
                case '?': {
                    manuPressSHIFTED(47);
                    break;
                }
                case '/': {
                    manuPress(47);
                    break;
                }
                case ':': {
                    manuPressSHIFTED(59);
                    break;
                }
                case ';': {
                    manuPress(59);
                    break;
                }
                case '.': {
                    manuPress(46);
                    break;
                }
                case ',': {
                    manuPress(44);
                    break;
                }
                case '"': {
                    manuPressSHIFTED(222);
                    break;
                }
                case '\'': {
                    manuPress(222);
                    break;
                }
                case '[': {
                    manuPress(91);
                    break;
                }
                case ']': {
                    manuPress(93);
                    break;
                }
                case '{': {
                    manuPressSHIFTED(91);
                    break;
                }
                case '}': {
                    manuPressSHIFTED(93);
                    break;
                }
                case '\\': {
                    manuPress(92);
                    break;
                }
                case '|': {
                    manuPressSHIFTED(92);
                    break;
                }
                case '=': {
                    manuPressSHIFTED(61);
                    break;
                }
                case '+': {
                    manuPressSHIFTED(61);
                    break;
                }
                case '-': {
                    manuPress(45);
                    break;
                }
                case '_': {
                    manuPressSHIFTED(45);
                    break;
                }
                case '`': {
                    manuPress(192);
                    break;
                }
                case '~': {
                    manuPressSHIFTED(192);
                    break;
                }
                case '<': {
                    manuPressSHIFTED(44);
                    break;
                }
                case '>': {
                    manuPressSHIFTED(46);
                    break;
                }

            }
        }

    }
    System.out.println("");
}

void type(String letters) {
    MYROBOT.setAutoDelay(TYPE_DELAY);
    char[] letter = letters.toCharArray();
    for (int i = 0; i < letter.length; i++) {
        System.out.print(letter[i]);
        try {
            if ((Character.isLowerCase(letter[i])) || (Character.isDigit(letter[i]))) {
                MYROBOT.keyPress(KEYMAP.get(letter[i]));
                MYROBOT.keyRelease(KEYMAP.get(letter[i]));
            } else {
                letter[i] = Character.toLowerCase(letter[i]);
                MYROBOT.keyPress(16);
                MYROBOT.keyPress(KEYMAP.get(letter[i]));
                MYROBOT.keyRelease(KEYMAP.get(letter[i]));
                MYROBOT.keyRelease(16);
            }
        } catch (Exception e) {
            switch (letter[i]) {
                case '!': {
                    manuPressSHIFTED(49);
                    break;
                }
                case '@': {
                    manuPressSHIFTED(50);
                    break;
                }
                case '#': {
                    manuPressSHIFTED(51);
                    break;
                }
                case '$': {
                    manuPressSHIFTED(52);
                    break;
                }
                case '%': {
                    manuPressSHIFTED(53);
                    break;
                }
                case '^': {
                    manuPressSHIFTED(54);
                    break;
                }
                case '&': {
                    manuPressSHIFTED(55);
                    break;
                }
                case '*': {
                    manuPressSHIFTED(56);
                    break;
                }
                case '(': {
                    manuPressSHIFTED(57);
                    break;
                }
                case ')': {
                    manuPressSHIFTED(48);
                    break;
                }
                case '?': {
                    manuPressSHIFTED(47);
                    break;
                }
                case '/': {
                    manuPress(47);
                    break;
                }
                case ':': {
                    manuPressSHIFTED(59);
                    break;
                }
                case ';': {
                    manuPress(59);
                    break;
                }
                case '.': {
                    manuPress(46);
                    break;
                }
                case ',': {
                    manuPress(44);
                    break;
                }
                case '"': {
                    manuPressSHIFTED(222);
                    break;
                }
                case '\'': {
                    manuPress(222);
                    break;
                }
                case '[': {
                    manuPress(91);
                    break;
                }
                case ']': {
                    manuPress(93);
                    break;
                }
                case '{': {
                    manuPressSHIFTED(91);
                    break;
                }
                case '}': {
                    manuPressSHIFTED(93);
                    break;
                }
                case '\\': {
                    manuPress(92);
                    break;
                }
                case '|': {
                    manuPressSHIFTED(92);
                    break;
                }
                case '=': {
                    manuPressSHIFTED(61);
                    break;
                }
                case '+': {
                    manuPressSHIFTED(61);
                    break;
                }
                case '-': {
                    manuPress(45);
                    break;
                }
                case '_': {
                    manuPressSHIFTED(45);
                    break;
                }
                case '`': {
                    manuPress(192);
                    break;
                }
                case '~': {
                    manuPressSHIFTED(192);
                    break;
                }
                case '<': {
                    manuPressSHIFTED(44);
                    break;
                }
                case '>': {
                    manuPressSHIFTED(46);
                    break;
                }

            }
        }

    }
    System.out.println("");
}

void typeln(String letters) {
    type(letters);
    enter();
}

void typelnHuman(String letters) {
    typeHuman(letters);
    enter();
}

void getRun() {
    MYROBOT.keyPress(524);
    MYROBOT.keyPress(82);
    MYROBOT.keyRelease(82);
    MYROBOT.keyRelease(524);
    sleep(500);
}

void combinePress(int keyOne, int keyTwo) {
    MYROBOT.keyPress(keyOne);
    MYROBOT.keyPress(keyTwo);
    MYROBOT.keyRelease(keyTwo);
    MYROBOT.keyRelease(keyOne);
}

void combinePress(int keyOne, int keyTwo, int keyThree) {
    MYROBOT.keyPress(keyOne);
    MYROBOT.keyPress(keyTwo);
    MYROBOT.keyPress(keyThree);
    MYROBOT.keyRelease(keyThree);
    MYROBOT.keyRelease(keyTwo);
    MYROBOT.keyRelease(keyOne);
}

void altTab() {
    MYROBOT.keyPress(18);
    MYROBOT.keyPress(9);
    MYROBOT.keyRelease(9);
    MYROBOT.keyRelease(18);
}

void winD() {
    MYROBOT.keyPress(524);
    MYROBOT.keyPress(68);
    MYROBOT.keyRelease(68);
    MYROBOT.keyRelease(524);
}

void altF4() {
    MYROBOT.keyPress(18);
    MYROBOT.keyPress(115);
    MYROBOT.keyRelease(115);
    MYROBOT.keyRelease(18);
}

void enter() {
    MYROBOT.keyPress(10);
    MYROBOT.keyRelease(10);
}

void backspace() {
    MYROBOT.keyPress(8);
    MYROBOT.keyRelease(8);
}

void sleep(long duration) {
    try {
        Thread.sleep(duration);
    } catch (Exception e) {
    }
}

void click() {
    MYROBOT.mousePress(16);
    MYROBOT.mouseRelease(16);
}

void doubleClick() {
    click();
    click();
}

void clickNhold(boolean tof) {
    if (tof) {
        MYROBOT.mousePress(16);
    } else {
        MYROBOT.mouseRelease(16);
    }
}

void rightClick() {
    MYROBOT.mousePress(4);
    MYROBOT.mouseRelease(4);
}

void rightclickHold(boolean tof) {
    if (tof) {
        MYROBOT.mousePress(4);
    } else {
        MYROBOT.mouseRelease(4);
    }
}

void middleClick() {
    MYROBOT.mousePress(8);
    MYROBOT.mouseRelease(8);
}

void manuPress(int code) {
    MYROBOT.keyRelease(16);
    MYROBOT.keyPress(code);
    MYROBOT.keyRelease(code);
}

void manuPressSHIFTED(int code) {
    MYROBOT.keyPress(16);
    MYROBOT.keyPress(code);
    MYROBOT.keyRelease(code);
    MYROBOT.keyRelease(16);
}

void gotoxy(int goX, int goY) {
    lastX = X;
    lastY = Y;
    MYROBOT.setAutoDelay(MOUSE_DELAY);
    int initX = X;
    int initY = Y;
    if (goX == X) {
        if (goY > Y) {
            for (int i = Y; i <= goY; i++) {
                Y = i;
                MYROBOT.mouseMove(X, Y);
            }
        } else {
            for (int i = Y; i >= goY; i--) {
                Y = i;
                MYROBOT.mouseMove(X, Y);
            }
        }
    } else if (goX > X) {
        for (int i = 0; i <= (goX - initX); i++) {
            X = initX + i;
            Y = initY + ((i * (goY - initY)) / (goX - initX));
            MYROBOT.mouseMove(X, Y);
        }

    } else {
        for (int i = 0; i >= (goX - initX); i--) {
            X = initX + i;
            Y = initY + ((i * (goY - initY)) / (goX - initX));
            MYROBOT.mouseMove(X, Y);
        }
    }
}

void getRGB(Color pixel) {
    RED = pixel.getRed();
    GREEN = pixel.getGreen();
    BLUE = pixel.getBlue();
    System.out.println(RED + "," + GREEN + "," + BLUE);
}

void savePoint() {
    savedX = X;
    savedY = Y;
}

void captureScreen() {
    System.out.println("Capturing...");
    SCREEN = new Color[SCREEN_X][SCREEN_Y];
    for (int i = 0; i < SCREEN_X; i++) {
        for (int j = 0; j < SCREEN_Y / 2; j++) {
            SCREEN[i][j] = MYROBOT.getPixelColor(i, j);
        }
    }
    System.out.println("Capturing done");
}

void gotoPixel(Color pixel) {
    if (MYROBOT.getPixelColor(X, Y).getRGB() != pixel.getRGB()) {
        for (int i = SCREEN_X - 4; i >= 0; i--) {
            for (int j = 3; j < SCREEN_Y / 2; j++) {
                if ((MYROBOT.getPixelColor(i, j).getRGB() == pixel.getRGB())) {
                    gotoxy(i, j);
                    i = -1;
                    break;
                }
            }
        }
    }
    if (MYROBOT.getPixelColor(X, Y).getRGB() == pixel.getRGB()) {
        while (MYROBOT.getPixelColor(X, Y).getRGB() == pixel.getRGB()) {
            gotoxy((X - 1), Y);
        }
    }
}

void emergeFromPixel(Color pixel) {
    if (MYROBOT.getPixelColor(X, Y).getRGB() != pixel.getRGB()) {
        for (int i = SCREEN_X - 4; i >= 0; i--) {
            for (int j = 3; j < SCREEN_Y / 2; j++) {
                if ((MYROBOT.getPixelColor(i, j).getRGB() == pixel.getRGB())) {
                    MYROBOT.mouseMove(i, j);
                    X = i;
                    Y = j;
                    i = -1;
                    break;
                }
            }
        }
    }
}

void shift(char dir) {
    int initX = X;
    Color initPixel;
    dir = Character.toUpperCase(dir);
    switch (dir) {
        case 'L': {
            gotoxy((X - 8), Y);
            initPixel = MYROBOT.getPixelColor(X, Y);
            while (MYROBOT.getPixelColor(X, Y).getRGB() == initPixel.getRGB()) {
                gotoxy((X - 1), Y);
            }
            initX = X;
            for (int i = initX; i > initX - 20; i--) {
                if (MYROBOT.getPixelColor(i, Y).getRGB() == initPixel.getRGB()) {
                    gotoxy(i, Y);
                    while (MYROBOT.getPixelColor(X, Y).getRGB() == initPixel.getRGB()) {
                        gotoxy((X - 1), Y);
                    }
                    break;
                }
            }
            break;
        }
        case 'R': {
            initPixel = MYROBOT.getPixelColor(X, Y);
            while (MYROBOT.getPixelColor(X, Y).getRGB() == initPixel.getRGB()) {
                gotoxy((X + 1), Y);
            }
            initX = X;
            for (int i = initX; i < initX + 20; i++) {
                if (MYROBOT.getPixelColor(i, Y).getRGB() == initPixel.getRGB()) {
                    gotoxy(i, Y);
                    while (MYROBOT.getPixelColor(X, Y).getRGB() == initPixel.getRGB()) {
                        gotoxy((X + 1), Y);
                    }
                    break;
                }
            }
            gotoxy((X + 8), Y);
            break;
        }
    }
}

void emergeFromButton(String butName) {
    int tempDelay = TYPE_DELAY;
    TYPE_DELAY = 0;
    MYROBOT.setAutoDelay(TYPE_DELAY);
    butName = butName.toUpperCase();
    if (butName.equals("CLOSE")) {
        emergeFromPixel(PIX_CLOSE_ACTIVE_OFF_MOUSE);
    } else if (butName.equals("MAXIMIZE")) {
        emergeFromPixel(PIX_CLOSE_ACTIVE_OFF_MOUSE);
        shift('L');
    } else if (butName.equals("MINIMIZE")) {
        emergeFromPixel(PIX_CLOSE_ACTIVE_OFF_MOUSE);
        shift('L');
        shift('L');
    } else if (butName.equals("START")) {
        MYROBOT.mouseMove(20, SCREEN_Y - 10);
    }
    TYPE_DELAY = tempDelay;
    MYROBOT.setAutoDelay(TYPE_DELAY);
}

void gotoButton(String butName) {
    butName = butName.toUpperCase();
    if (butName.equals("CLOSE")) {
        if (MYROBOT.getPixelColor(X, Y).getRGB() != PIX_CLOSE_ACTIVE_ON_MOUSE.getRGB()) {
            gotoPixel(PIX_CLOSE_ACTIVE_OFF_MOUSE);
        }
    } else if (butName.equals("MAXIMIZE")) {
        gotoPixel(PIX_CLOSE_ACTIVE_OFF_MOUSE);
        shift('L');
    } else if (butName.equals("MINIMIZE")) {
        gotoPixel(PIX_CLOSE_ACTIVE_OFF_MOUSE);
        shift('L');
        shift('L');
    } else if (butName.equals("START")) {
        gotoxy(20, SCREEN_Y - 10);
    }
}

void catchTitleBar() {
    gotoButton("CLOSE");
    gotoxy(X - 150, Y);
}

void pressKey(String keyName) {
    keyName = keyName.toUpperCase();
    if (keyName.equals("ESC")) {
        manuPress(27);
    }
}

void closeAllWindows() {
    while ((MYROBOT.getPixelColor(65, SCREEN_Y - 35).getRed() > 213) && (MYROBOT.getPixelColor(65, SCREEN_Y - 35).getGreen() > 220) && (MYROBOT.getPixelColor(65, SCREEN_Y - 35).getBlue() > 228)) {
        altTab();
        gotoButton("CLOSE");
        sleep(100);
        click();
        sleep(50);
    }
}

void quickCloseAllWindows() {
    while ((MYROBOT.getPixelColor(65, SCREEN_Y - 35).getRed() > 213) && (MYROBOT.getPixelColor(65, SCREEN_Y - 35).getGreen() > 220) && (MYROBOT.getPixelColor(65, SCREEN_Y - 35).getBlue() > 228)) {
        winD();
        sleep(100);
        altTab();
        altF4();
        type("n");
    }
}

void getNotepad() {
    int saveDelay = TYPE_DELAY;
    TYPE_DELAY = 0;
    MYROBOT.setAutoDelay(0);
    MYROBOT.keyPress(524);
    MYROBOT.keyPress(82);
    MYROBOT.keyRelease(82);
    MYROBOT.keyRelease(524);
    sleep(200);
    typeln("notepad");
    sleep(500);
    TYPE_DELAY = saveDelay;
    MYROBOT.setAutoDelay(TYPE_DELAY);
}

void closeActiveWindow() {
    gotoButton("CLOSE");
    click();
    type("n");
}

void openMyComputer() {
    gotoxy(50, 50);
    doubleClick();
}

void lockMouse() {
    Thread lockMouse = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                MYROBOT.mouseMove(X, Y);
                sleep(100);
            }
        }
    });
    lockMouse.start();
}

让我知道你的进步。希望这会有所帮助...