问题描述
我想使用机器人单击鼠标按钮4,侧面按钮.
I want to use Robot to click mouse button 4, side button.
InputEvent
仅具有3个标准的左,中(滚动)和右按钮.
The InputEvent
only have the 3 standard left, middle (scroll) and right buttons.
InputEvent.BUTTON1_DOWN_MASK = 1024
InputEvent.BUTTON2_DOWN_MASK = 2048
InputEvent.BUTTON3_DOWN_MASK = 4096
所以我尝试传递公式并将数字8192
So I tried to flow the formula and send to the Robot
the number 8192
public static void main(String[] args)
{
try
{
Robot mouseHandler = new Robot();
mouseHandler.mousePress(8192);
mouseHandler.mouseRelease(8192);
} catch (AWTException e)
{
e.printStackTrace();
}
}
但是它不起作用(按预期方式)并引发异常:
but it didn't work (as expected) and throws an exception:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid combination of button flags
at java.awt.Robot.checkButtonsArgument(Robot.java:320)
at java.awt.Robot.mousePress(Robot.java:256)
at controller_client.MainClass.main(MainClass.java:30)
是否可以通过按钮4创建鼠标单击?
Is it possible to create a mouse click with button 4?
推荐答案
好吧,所以在搜索更多内容后,我发现此函数返回了从1到20 MouseEvent.getMaskForButton(int button)
的任何鼠标按钮掩码.
Ok so after searching more I found this function that return any mouse button mask from 1 to 20 MouseEvent.getMaskForButton(int button)
.
尝试后,Robot
类确实设法单击了button4和button5,侧面按钮,像这样:
After trying it the Robot
class did manage to click the button4 and button5, side buttons, like so:
try
{
Robot mouseHandler = new Robot();
int mouseButtonNum = 4; // 1 - 20
// but only buttons from 1 to 5 did work with Robot
mouseHandler.mousePress(MouseEvent.getMaskForButton(mouseButtonNum));
mouseHandler.mouseRelease(MouseEvent.getMaskForButton(mouseButtonNum));
} catch (AWTException e)
{
e.printStackTrace();
}
我使用了带有3个按钮的鼠标,而Robot
确实设法单击了4和5个按钮.但是Robot
似乎只能单击1到5之间的按钮,因此可能气垫船充满鳗鱼的解释是正确的:
I used a mouse with 3 buttons and Robot
did manage to click 4 and 5 buttons. But it seems like that Robot
only can click buttons from 1 to 5, soprobably Hovercraft Full Of Eels's explanation is correct:
如果他操作正确,则我使用的操作系统是Windows10.如果某人安装了Linux
,并且他知道如何将更多鼠标按钮定位到Linux
,并试图使Robot
单击5上方的鼠标按钮,那么请注意我是否有效.
If he does right then the OS that I use is Windows 10. If someone have Linux
and he knows how to address more mouse buttons to Linux
and tried to make Robot
click mouse button above 5 so please note me if it works or not.
这篇关于Java Robot单击侧面按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!