3

我想在 Java 中模拟 mousePressed 事件,我发现我可以为此使用Robot该类,并且它可以工作,但仅在 Windows 中而不在 Mac OS X 中。

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

这是我使用的代码:

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

3 回答 3

2

如果你想模拟 a 上的点击动作,JButton你可以调用该doClick方法,看看这里。否则,也许这个类似的问题可以帮助你。希望这可以帮助。

于 2011-04-03T11:37:18.283 回答
1

通过检查使用 java.awt.robot.mousePress(int button) 无法在 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);  
于 2014-01-18T02:18:28.017 回答
0

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

private final class ContractMouseAdapter extends MouseAdapter {

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

}

并在您的 Swing 代码中将此适配器称为

MouseAdapter mouseAction = new ContractMouseAdapter(Component);
于 2011-04-07T18:33:18.833 回答