Replicating Mouse behavior in Code

安稳与你 提交于 2020-01-07 05:47:07

问题


This is related to question: Focus problems with JDK7 and native components.

While working on workarounds we noticed that if we clicked on another component on the window (i.e. a label showing a picture) and then click on the text fields (within the Flash application), everything seemed to work fine. So I've been trying to reproduce that from code but haven't been successful.

Basically, when the mouse is detected hovering over the text box I get notified from the Flash program and I request focus on the label, so when the user clicks on the actual field the label already has the focus.

I request focus like this:

draggableComponent.requestFocus();

Where draggableComponent is the label I've been talking about. I guess this is not equivalent to clicking on the label. What I'm missing?


回答1:


Finally I found the answer here.

The following example shows how to simulate mouse and key presses in Java using java.awt.Robot class.

try {
    Robot robot = new Robot();

    // Simulate a mouse click
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);

    // Simulate a key press
    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);
} catch (AWTException e) {
    e.printStackTrace();
}

The Robot class gave me everything I needed.



来源:https://stackoverflow.com/questions/16742540/replicating-mouse-behavior-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!