Search code examples
javajna

How can i send click mouse and keyboard events to a flash plugin running in firefox when the firefox window runs in background with JNA?


I want to program a bot in Java which controls a flash program running in firefox. Therefore i have to send mouse and keyboard events to it. On my research i found JNA and already used it for creating a screenshot of the firefox window(it also works of course when firefox is in background, i have the handle of the firefox window). After that i started with some easy mouse events like a simple left click(see code below), but i just doesn't work. I tested it with an own simple window and the events i sent to it were processed by the JFrame's MouseListener and not by for example the JButton's ActionListener.

User32 user32 = (User32) Native.loadLibrary("user32", User32.class,
            W32APIOptions.DEFAULT_OPTIONS);

    getRightWindowHandle(user32);

    System.out.println("Got right handle");

    WinDef.HWND hwnd = rightWindowHandle;
    Thread.sleep(1000);

    long y = 20 + (20 << 16);// x + (y << 16)
    WinDef.LPARAM l = new WinDef.LPARAM(y);
    WinDef.WPARAM w = new WinDef.WPARAM(0);
    user32.PostMessage(hwnd, WM_LBUTTONDOWN, w, l);
    System.out.println("Message posted");
    Thread.sleep(1000);
    user32.PostMessage(hwnd, WM_LBUTTONUP, w, l);

Unluckily I am a absolute beginner with JNA so i have no idea how i can make the events be processed by the components on the window and not by the window itself. Can anyone provide me a short code snippet which does this? I am also happy about any links or keywords i can search for which help me understanding and removing this error.

It's also possible that the mistake is somewhere else but for me it looks like this was the mistake.


Solution

  • ok solved the problem now: instead of the window handle of the firefox window i used the window handle of the plash player in firefox. I got it with Spy++ and the enumChildWindows() function.