在代码中复制鼠标行为 [英] Replicating Mouse behavior in Code

查看:127
本文介绍了在代码中复制鼠标行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与问题:焦点与JDK7和本地组件的问题

我们注意到,如果我们点击窗口上的另一个组件(即显示图片的标签),然后点击文本字段(在Flash应用程序中),一切似乎工作正常。所以我一直试图从代码中重现,但一直没有成功。

基本上,当检测到鼠标悬停在文本框,我得到Flash程序和我请求关注的标签,所以当用户点击实际的领域时,标签已经有焦点。



我要求的焦点是这样的:

  draggableComponent.requestFocus(); 

其中draggableComponent是我一直在谈论的标签。我想这不等于点击标签。我错过了什么?

解决方案

最后,我找到了答案 here

<下面的例子展示了如何使用java.awt.Robot类模拟Java中的鼠标和键盘按键。

  try {
机器人机器人=新机器人();

//模拟鼠标点击
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

//模拟按键
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
catch(AWTException e){
e.printStackTrace();

机器人课给了我所需要的一切。


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?

解决方案

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.

这篇关于在代码中复制鼠标行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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