1

我正在尝试从 FireFox 窗口控制一些 Java 游戏。如何将键和鼠标事件发送到该 Java 小程序?

如果这很重要,我正在使用 Windows XP。

编辑:即使我在这里有标签,我也不想用 Java 来做这件事。c++ 解决方案将是最佳的。

4

2 回答 2

2

您可以尝试使用Robot,但这在 FireFox 中可能不起作用。您还可以使用 abstractbutton.doClick() 之类的方法

如果 Robot 不起作用,您可以通过在组件上设置文本来合成关键事件,以及您可以使用 doClick() 和 requestFocus() 的鼠标事件

如果这些都不起作用,您也许可以使用 javascript 和 html 页面来实现您的目标。

于 2012-04-21T15:48:05.853 回答
0

以下是适用于击键的内容:

这两个动作的推荐方法都是使用SendInput 这个网站非常适合开始了解 sendinput

要查找 windows 目标,请使用Spy++文档

但我确实有以下其他示例:

这里的示例是使用postmessage的记事本。

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
        }
    }

    return 0;
}

您可能还想查看windows hooks,它可以发送鼠标输入或User32 mouse_event

[DllImport("User32.Dll")]
private static extern void mouse_event(UInt32 dwFlags, int dx, int dy, UInt32 dwData, int dwExtraInfo);

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y); 

public enum MouseEventFlags
{
    LEFTDOWN = 0x00000002,
    LEFTUP = 0x00000004,
    MIDDLEDOWN = 0x00000020,
    MIDDLEUP = 0x00000040,
    MOVE = 0x00000001,
    ABSOLUTE = 0x00008000,
    RIGHTDOWN = 0x00000008,
    RIGHTUP = 0x00000010
}

public static void SendLeftClick(int X, int Y)
{
    mouse_event((uint)MouseEventFlags.LEFTDOWN, 0, 0, 0, 0);
    mouse_event((uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
}
于 2012-04-21T15:50:36.597 回答