首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java Robot 類完整指南:程式化控制滑鼠鍵盤實現自動化測試與操作

     編輯:Java 程式設計入門

前言

java.awt.Robot 類提供程式化控制滑鼠與鍵盤的能力,廣泛應用於自動化測試、桌面自動化腳本、演示錄製等場景。一旦取得控制權,Java 代碼即可模擬任意滑鼠鍵盤操作。

基礎範例:模擬鍵盤輸入 "HI CAOER"

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class RobotExp {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            // 5秒延遲,留時間開啟記事本
            robot.delay(5000);
            
            robot.keyPress(KeyEvent.VK_H);
            robot.keyPress(KeyEvent.VK_I);
            robot.keyPress(KeyEvent.VK_SPACE);
            robot.keyPress(KeyEvent.VK_C);
            robot.keyPress(KeyEvent.VK_A);
            robot.keyPress(KeyEvent.VK_O);
            robot.keyPress(KeyEvent.VK_E);
            robot.keyPress(KeyEvent.VK_R);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}

注意:keyPress 只模擬按下,實際應用需配合 keyRelease 釋放按鍵,否則系統會視為長按。

進階封裝:網友優化版

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class RobotExp {
    // 單鍵按下並釋放
    public static void pressKey(Robot robot, int keyvalue) {
        robot.keyPress(keyvalue);
        robot.keyRelease(keyvalue);
    }
    
    // Shift 組合鍵
    public static void pressKeyWithShift(Robot robot, int keyvalue) {
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(keyvalue);
        robot.keyRelease(keyvalue);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    }
    
    // 關閉應用程式 (Alt+F4)
    public static void closeApplication(Robot robot) {
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_F4);
        robot.keyRelease(KeyEvent.VK_ALT);
        robot.keyRelease(KeyEvent.VK_F4);
        // Linux 替代: Alt+W
        robot.keyPress(KeyEvent.VK_N); // 確認不儲存
        robot.keyRelease(KeyEvent.VK_N);
    }
    
    public static void main(String[] args) throws IOException {
        try {
            Robot robot = new Robot();
            Runtime.getRuntime().exec("notepad"); // 啟動記事本
            robot.delay(3000);
            
            for (int i = 0; i < 100; i++) {
                pressKeyWithShift(robot, KeyEvent.VK_H); // H
                pressKey(robot, KeyEvent.VK_I);          // i
                pressKey(robot, KeyEvent.VK_SPACE);      // 空格
                pressKeyWithShift(robot, KeyEvent.VK_I); // I
                pressKey(robot, KeyEvent.VK_SPACE);
                pressKey(robot, KeyEvent.VK_A);
                pressKey(robot, KeyEvent.VK_M);
                pressKey(robot, KeyEvent.VK_SPACE);
                pressKey(robot, KeyEvent.VK_T);
                pressKey(robot, KeyEvent.VK_H);
                pressKey(robot, KeyEvent.VK_E);
                pressKey(robot, KeyEvent.VK_SPACE);
                pressKey(robot, KeyEvent.VK_J);
                pressKey(robot, KeyEvent.VK_A);
                pressKey(robot, KeyEvent.VK_V);
                pressKey(robot, KeyEvent.VK_A);
                pressKey(robot, KeyEvent.VK_SPACE);
                pressKey(robot, KeyEvent.VK_R);
                pressKey(robot, KeyEvent.VK_O);
                pressKey(robot, KeyEvent.VK_B);
                pressKey(robot, KeyEvent.VK_O);
                pressKey(robot, KeyEvent.VK_T);
                pressKey(robot, KeyEvent.VK_ENTER);
            }
            closeApplication(robot);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}

核心 API 速查

方法說明
keyPress(int keycode)模擬按下指定鍵
keyRelease(int keycode)模擬釋放指定鍵
mouseMove(int x, int y)移動滑鼠至絕對座標
mousePress(int buttons)按下滑鼠鍵 (InputEvent.BUTTON1_MASK 等)
mouseRelease(int buttons)釋放滑鼠鍵
delay(int ms)暫停指定毫秒
createScreenCapture(Rectangle rect)截取螢幕區域

實戰應用場景

  • 自動化 GUI 測試:模擬使用者操作流程,驗證介面行為
  • 桌面自動化腳本:批次處理重複性操作(如檔案轉換、資料錄入)
  • 演示/教學錄製:程式化控制演示流程,確保可重現性
  • 無障礙輔助工具:為行動不便使用者提供替代輸入方式

注意事項與最佳實踐

  1. 必須配對使用 keyPress/keyRelease,避免按鍵卡住
  2. 適當延遲 (delay) 確保目標應用程式就緒
  3. 異常處理:捕獲 AWTException(無法建立 Robot 實例時拋出)
  4. 跨平台差異:快捷鍵組合在 Windows/Linux/macOS 不同,需條件編譯或配置
  5. 安全權限:簽名 Applet 或本地應用才能使用 Robot,Web Start 需 all-permissions
  6. 測試穩定性:GUI 自動化測試易碎,建議配合圖像識別或元件檢查增強健壯性
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved