Java Robot单击侧面按钮 [英] Java Robot click side button

查看:274
本文介绍了Java Robot单击侧面按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用机器人单击鼠标按钮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, so probably Hovercraft Full Of Eels's explanation is correct:

我还想知道您的问题不仅是特定于操作系统的问题,还是特定于供应商的问题,因为我不知道大多数常见操作系统是否已完全解决了处理多余甚至不寻常的鼠标按钮的问题.

I also have to wonder if your issue is not only OS-specific, but also vendor-specific, since I don't know if handling of extra and perhaps unusual mouse buttons has been fully addressed by most common OS's.

如果他操作正确,则我使用的操作系统是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单击侧面按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆