Rookie
Rookie

Reputation: 4134

How to send key and mouse events to a Java applet?

I'm trying to control some Java game from FireFox window. How can I send key and mouse events to that Java applet?

I'm using Windows XP if that matters.

Edit: I'm not trying to do this with Java even though i have the tag here. A c++ solution would be optimal.

Upvotes: 1

Views: 1157

Answers (2)

ControlAltDel
ControlAltDel

Reputation: 35011

You might try using Robot, but this might not work in FireFox. You can also use methods like abstractbutton.doClick()

If Robot doesn't work, key events you can synthesize by just setting text on a component, and mouse events you can use doClick() and requestFocus()

If none of that works, you might be able to accomplish your goals working with javascript and an html page.

Upvotes: 2

Serdalis
Serdalis

Reputation: 10489

Here is something that will work for keystrokes:

The recommended methods for both these actions are using SendInput This website is perfect for beginning to understand sendinput

To find windows targets use Spy++, documentation

but I do have other examples below:

Example here is for Notepad using 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;
}

You may also like to look at windows hooks, which can send mouse input Or 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);
}

Upvotes: 0

Related Questions