Java模拟鼠标和键盘输入

2020-12-01  本文已影响0人  蓝不蓝编程

用途

在电脑(Windows/Mac)上模拟鼠标和键盘输入
Mac运行需要打开相关权限,详见文末说明.

效果图

代码

import java.awt.*;
import java.awt.event.KeyEvent;

class RobotDemo {

    private Robot robot = null;

    public RobotDemo() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟键盘输入abc
     */
    public void keyBoardDemo() {
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);

        robot.keyPress(KeyEvent.VK_B);
        robot.keyRelease(KeyEvent.VK_B);

        robot.keyPress(KeyEvent.VK_C);
        robot.keyRelease(KeyEvent.VK_C);
    }

    /**
     * 移动鼠标位置
     */
    public void mouseDemo() {
        robot.mouseMove(200, 200);

        robot.mousePress(KeyEvent.BUTTON1_MASK);
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        robot.mouseRelease(KeyEvent.BUTTON1_MASK);
    }

    public void delay(int milliseconds) {
        robot.delay(milliseconds);
    }

    public static void main(String[] args) {
        RobotDemo demo = new RobotDemo();
//        demo.delay(2000);
        demo.mouseDemo();
        demo.keyBoardDemo();
    }
}

Mac运行说明

Mac对程序模拟操作做了权限限制,需要先打开相关权限,操作步骤如下:

  1. 到“系统偏好设置”中打开“安全性与隐私”;
  2. 选中“隐私”页签,左侧选中“辅助功能”;
  3. 先解锁后勾选待运行本程序的应用;



更高级的代码

支持模拟输入键盘上大部分的键(Kotlin代码).

import java.awt.Robot
import java.awt.event.KeyEvent


fun main() {
    val keyboard = Keyboard()
    keyboard.delay(2000)
    keyboard.type(
        "hahaha\n"
    )
}

class Keyboard {
    private val robot: Robot = Robot()
    fun delay(milliSeconds: Int) {
        robot.delay(milliSeconds)
    }

    fun type(characters: CharSequence) {
        val length = characters.length
        for (i in 0 until length) {
            val character = characters[i]
            type(character)
        }
    }

    private fun type(char: Char) {
        when (char) {
            'a' -> doType(KeyEvent.VK_A)
            'b' -> doType(KeyEvent.VK_B)
            'c' -> doType(KeyEvent.VK_C)
            'd' -> doType(KeyEvent.VK_D)
            'e' -> doType(KeyEvent.VK_E)
            'f' -> doType(KeyEvent.VK_F)
            'g' -> doType(KeyEvent.VK_G)
            'h' -> doType(KeyEvent.VK_H)
            'i' -> doType(KeyEvent.VK_I)
            'j' -> doType(KeyEvent.VK_J)
            'k' -> doType(KeyEvent.VK_K)
            'l' -> doType(KeyEvent.VK_L)
            'm' -> doType(KeyEvent.VK_M)
            'n' -> doType(KeyEvent.VK_N)
            'o' -> doType(KeyEvent.VK_O)
            'p' -> doType(KeyEvent.VK_P)
            'q' -> doType(KeyEvent.VK_Q)
            'r' -> doType(KeyEvent.VK_R)
            's' -> doType(KeyEvent.VK_S)
            't' -> doType(KeyEvent.VK_T)
            'u' -> doType(KeyEvent.VK_U)
            'v' -> doType(KeyEvent.VK_V)
            'w' -> doType(KeyEvent.VK_W)
            'x' -> doType(KeyEvent.VK_X)
            'y' -> doType(KeyEvent.VK_Y)
            'z' -> doType(KeyEvent.VK_Z)
            'A' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A)
            'B' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B)
            'C' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C)
            'D' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D)
            'E' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E)
            'F' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F)
            'G' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G)
            'H' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H)
            'I' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I)
            'J' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J)
            'K' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K)
            'L' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L)
            'M' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M)
            'N' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N)
            'O' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O)
            'P' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P)
            'Q' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q)
            'R' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R)
            'S' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S)
            'T' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T)
            'U' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U)
            'V' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V)
            'W' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W)
            'X' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X)
            'Y' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y)
            'Z' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z)
            '`' -> doType(KeyEvent.VK_BACK_QUOTE)
            '0' -> doType(KeyEvent.VK_0)
            '1' -> doType(KeyEvent.VK_1)
            '2' -> doType(KeyEvent.VK_2)
            '3' -> doType(KeyEvent.VK_3)
            '4' -> doType(KeyEvent.VK_4)
            '5' -> doType(KeyEvent.VK_5)
            '6' -> doType(KeyEvent.VK_6)
            '7' -> doType(KeyEvent.VK_7)
            '8' -> doType(KeyEvent.VK_8)
            '9' -> doType(KeyEvent.VK_9)
            '-' -> doType(KeyEvent.VK_MINUS)
            '=' -> doType(KeyEvent.VK_EQUALS)
            '~' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE)
            '!' -> doType(KeyEvent.VK_EXCLAMATION_MARK)
            '@' -> doType(KeyEvent.VK_AT)
            '#' -> doType(KeyEvent.VK_NUMBER_SIGN)
            '$' -> doType(KeyEvent.VK_DOLLAR)
            '%' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5)
            '^' -> doType(KeyEvent.VK_CIRCUMFLEX)
            '&' -> doType(KeyEvent.VK_AMPERSAND)
            '*' -> doType(KeyEvent.VK_ASTERISK)
//            '(' -> doType(KeyEvent.VK_LEFT_PARENTHESIS)
            '(' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_9)
//            ')' -> doType(KeyEvent.VK_RIGHT_PARENTHESIS)
            ')' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_0)
            '_' -> doType(KeyEvent.VK_UNDERSCORE)
            '+' -> doType(KeyEvent.VK_PLUS)
            '\t' -> doType(KeyEvent.VK_TAB)
            '\n' -> doType(KeyEvent.VK_ENTER)
            '[' -> doType(KeyEvent.VK_OPEN_BRACKET)
            ']' -> doType(KeyEvent.VK_CLOSE_BRACKET)
            '\\' -> doType(KeyEvent.VK_BACK_SLASH)
            '{' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET)
            '}' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET)
            '|' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH)
            ';' -> doType(KeyEvent.VK_SEMICOLON)
//            ':' -> doType(KeyEvent.VK_COLON)
            ':' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SEMICOLON)
            '\'' -> doType(KeyEvent.VK_QUOTE)
//            '"' -> doType(KeyEvent.VK_QUOTEDBL)
            '"' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_QUOTE)
            ',' -> doType(KeyEvent.VK_COMMA)
            '<' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_COMMA)
            '.' -> doType(KeyEvent.VK_PERIOD)
            '>' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_PERIOD)
            '/' -> doType(KeyEvent.VK_SLASH)
            '?' -> doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH)
            ' ' -> doType(KeyEvent.VK_SPACE)
            else -> throw IllegalArgumentException("Cannot type character $char")
        }
    }

    private fun doType(vararg keyCodes: Int) {
        doType(keyCodes, 0, keyCodes.size)
    }

    private fun doType(keyCodes: IntArray, offset: Int, length: Int) {
        if (length == 0) {
            return
        }
        robot.keyPress(keyCodes[offset])
        doType(keyCodes, offset + 1, length - 1)
        robot.keyRelease(keyCodes[offset])
    }
}
上一篇下一篇

猜你喜欢

热点阅读