繁体   English   中英

如何在不使用java.awt.robot的情况下模拟mousePressed事件?

[英]How can I simulate a mousePressed event without using java.awt.robot?

我想在Java中模拟一个mousePressed事件,我发现我可以使用Robot类,它可以工作,但只能在Windows中使用,而不能在Mac OS X中使用。

有没有人知道模拟mousePressed事件的另一种方法?

这是我使用的代码:

Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK);

如果要模拟JButton上的单击操作,可以调用doClick方法,请查看此处 否则,也许这个类似的问题可以帮助你。 希望这可以帮助。

我使用java.awt.robot.mousePress(int按钮)不能在mac os x 10.8上工作时遇到同样的问题

int b = InputEvent.getMaskForButton(MouseEvent.BUTTON1); //1024  
int c = InputEvent.BUTTON1_MASK; //8  
// works on mac  
Robot r = new Robot();  
r.mouseMove(500, 500);  
r.mousePress(1024);  
r.mouseRelease(1024);  

这是一个有用的示例代码。

private final class ContractMouseAdapter extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
        // Do whatever you want.
    }

}

并在你的Swing代码中调用此适配器

MouseAdapter mouseAction = new ContractMouseAdapter(Component);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM