gpt4 book ai didi

java - 使用 mousePress() 方法时出错

转载 作者:行者123 更新时间:2023-11-30 03:52:22 25 4
gpt4 key购买 nike

我正在尝试在按钮 4 上使用鼠标事件。使用

时返回的值
int moused = MouseInfo.getNumberOfButtons();

方法是5。除了右键单击按钮4之外,我不知道还有什么按钮;运行代码我收到此错误:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid combinati
on of button flags
at java.awt.Robot.checkButtonsArgument(Robot.java:324)
at java.awt.Robot.mousePress(Robot.java:260)
at RobotExp4.main(RobotExp4.java:23)

import java.awt.*;
import java.awt.event.KeyEvent;
public class RobotExp4{
public static void main(String [] args){
try{
Robot robot = new Robot();
System.out.println("7 Seconds after this message appears the robot will start to open a browser and make a search.");
robot.delay(7000);
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.delay(150);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
robot.delay(150);
robot.keyPress(KeyEvent.VK_D);
robot.keyRelease(KeyEvent.VK_D);
robot.delay(5000);
robot.mouseMove(1340, 192);
robot.delay(200);
robot.mousePress(5);
robot.delay(75);
robot.mouseRelease(5);

}catch(AWTException e){
e.printStackTrace();
}
}
}

最佳答案

您需要传递mousePress调用InputEvent.getMaskForButton的结果,像这样:

int mask4 = InputEvent.getMaskForButton(4);
robot.mousePress(mask4);
robot.delay(75);
robot.mouseRelease(mask4);

您需要传递掩码而不是按钮编号的原因是为了支持按钮组合:

int mask1_and_2 = InputEvent.getMaskForButton(1) | InputEvent.getMaskForButton(2);

上面的掩码对应于同时按下按钮 1 和 2。

关于java - 使用 mousePress() 方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24106182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com