java mousepress,java.awt.Robot中的鼠标preSS是没有任何影响

在尝试使用AWT.Robot类在Windows 8上模拟鼠标左键点击时,发现某些程序无法识别该操作。尽管代码正确地执行了mouseMove、mousePress和mouseRelease,但在特定程序中点击无效。尝试了Thread.sleep()延迟并未解决问题。可能需要考虑使用JNI和本地Windows API来解决这个问题。
摘要由CSDN通过智能技术生成

I'm trying to simulate a left click in a different program using awt.robot, with the following code:

int mask = InputEvent.BUTTON1_DOWN_MASK;

bot.mouseMove(x, y);

bot.mousePress(mask);

bot.mouseRelease(mask);

While this is moving the mouse to the correct part of the screen, the other program doesn't seem to be receiving the click. What am I doing wrong?

In certain other programs, this same method produces the click, but in one program, it has no effect.

I have tried Thread.sleep() to wait 30-300 milliseconds between mouseMove and mousePress, but it had no effect.

Do I need to use JNI and native windows API to achieve this? I'm on Windows 8.

解决方案

You are using the wrong mask: InputEvent.BUTTON1_MASK

JavaRobot类详解 概述 概述 JavaRobot类位于java.awt.Robot,该类⽤于为测试⾃动化,⾃运⾏演⽰程序和其他需要控制⿏标和键盘的应⽤程序⽣成本机系统输⼊ 事件,Robot类的主要⽬的是便于Java平台实现⾃动测试。 Robot可以模拟⿏标和键盘的输⼊,相当于Java版的按键精灵。 常⽤⽅法 常⽤⽅法 1.创建实例 Robot robot = new Robot(); 2.延时函数 robot.delay(毫秒值); 3.⿏标按下 robot.mousePress(⿏标上的按键); //⿏标左键:InputEvent.BUTTON1_DOWN_MASK //⿏标键:InputEvent.BUTTON2_DOWN_MASK //⿏标右键:InputEvent.BUTTON3_DOWN_MASK 4.⿏标释放 robot.mouseRelease(⿏标上的按键); //⿏标左键:InputEvent.BUTTON1_DOWN_MASK //⿏标键:InputEvent.BUTTON2_DOWN_MASK //⿏标右键:InputEvent.BUTTON3_DOWN_MASK 5.⿏标移动 //⿏标移动到指定位置 robot.mouseMove(int x,int y); 6.⿏标滚轮滑动 //⿏标滚动(参数⼩于0,表⽰向上滚动;参数⼤于0,表⽰向下滚动) robot.mouseWheel(int wheelAmt); 7.键盘按下指定的键 //键盘按下指定的键-----keycode:键盘键值常量,定义在KeyEvent.VK_XXX //KeyEvent.VK_WINDOWS:键盘上的windows键 //KeyEvent.VK_CONTROL:键盘上的ctrl键 //KeyEvent.VK_L:键盘上的L键 robot.keyPress(int keycode); 8.键盘释放指定的键 //键盘按下指定的键-----keycode:键盘键值常量,定义在KeyEvent.VK_XXX //KeyEvent.VK_WINDOWS:键盘上的windows键 //KeyEvent.VK_CONTROL:键盘上的ctrl键 //KeyEvent.VK_L:键盘上的L键 robot.keyRelease(int keycode); 9.获取屏幕指定坐标处像素颜⾊ //获取指定坐标处的像素颜⾊ Color color=robot.getPixelColor(int x,int y); 10.截取指定区域的图像(截图功能) //获取指定矩形区域的图像(截图) BufferedImage bufferedimage=robot.createScreenCapture(Rectangle screenRect); ⽰例:截取指定矩形区域的图像,并保存到指定的路径 public static void main(String[] args) throws AWTException, IOException { Robot robot=new Robot(); //获取指定矩形区域的屏幕图像 BufferedImage bufferedImage=robot.createScreenCapture(new Rectangle(100,100,500,500)); File f=new File("D:\\save.jpg"); OutputStream os=new FileOutputStream(f); ImageIO.write(bufferedImage, "jpg", os); } ⽰例:在指定区域⾃动输⼊指定字符 robot.mouseMove(342, 626); robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); robot.delay(500); robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_L); robot.delay(500); robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); robot.delay(500); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.delay(500); robot.keyPress(KeyEvent.VK_E); robot.keyRelease(KeyEvent.VK_E)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值