复制鼠标事件

huangapple 未分类评论48阅读模式
标题翻译

Replicating MouseEvents

问题

我正试图在一个小程序内复制鼠标事件,例如基于 x/y 坐标的点击。我目前已经实现了一个 MouseListener,用于记录鼠标移动、按下、释放等操作,但无论我尝试以编程方式复制鼠标事件,比如在 x/y 处点击,事件都不会被触发。

以下是我尝试复制点击 x/y 处的 MouseEvent 的示例:

applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
                MouseEvent.MOUSE_PRESSED,
                System.currentTimeMillis() + 10,
                MouseEvent.BUTTON1,
               x,y,
                0,
                false));

applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
                MouseEvent.MOUSE_RELEASED,
                System.currentTimeMillis() + 10,
                MouseEvent.BUTTON1,
               x,y,
                0,
                false));

但是没有任何反应... 我已经在多个 StackOverflow 帖子中搜索过,但似乎找不到解决方案。我还找到另一个解决方案,即使用 Java.awt.Robot,但这会控制系统鼠标,而我正试图避免这种情况。

英文翻译

I'm trying to replicate mouse events inside a applet, such as click based on x/y coords. I currently have a MouseListener implemented which records mouse movements, press, release etc but whenever I try to programmatically replicate mouse events such as Click at x/y the event is never triggered?

Example of a MouseEvent I've tried to replicate click at x/y:

applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
                MouseEvent.MOUSE_PRESSED,
                System.currentTimeMillis() + 10,
                MouseEvent.BUTTON1,
               x,y,
                0,
                false));

        applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
                MouseEvent.MOUSE_RELEASED,
                System.currentTimeMillis() + 10,
                MouseEvent.BUTTON1,
               x,y,
                0,
                false));

But nothing is registered.. I've searched through multiple stackoverflow posts and just can't seem to find the solution. There's another solution I found and that's using Java.awt.Robot but that takes control of system mouse, which I'm trying to avoid.

huangapple
  • 本文由 发表于 2020年5月30日 13:47:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/62098370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定