1

I've tried many options to send a keystroke using JNA but none works, I'm not able to make work the method SendMessageTimeout of the User32.dll using JNA. (jna-4.2.1.jar, jna-platform-4.2.1.jar)

That method doesn't seem to do anything, not error found, but not character sent either.

The User32.dll library works fine, the GetForegroundWindow and the SetForegroundWindow methods works like a charm.

But the SendMessageTimeout isn't working at all.

I think the problem is that I'm sending bad params to the function. Probably one of those or both:

    long lParamSend = 0;
    int fuFlagsSend = 0x0000;

But I don't know how to do it properly.

I've searched many times through the API: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644952(v=vs.85).aspx . But there is something that I'm missing.

Any help would be nice, here is the full code of my test:

    final User32 lib = User32.INSTANCE;

    System.out.println("Choose ForegroundWindow to test.");
    Thread.sleep(3000);
    // hWnd [in]
    HWND hnwdSend = lib.GetForegroundWindow();
    System.out.println("Pause");
    Thread.sleep(2000);
    lib.SetForegroundWindow(hnwdSend);
    System.out.println("Test send message.");

    int msgSend = 0x0100; // Press
    long wParamSend = 0x53; // Letter s.
    long lParamSend = 0;
    int fuFlagsSend = 0x0000;
    int uTimeoutSend = 5000;
    DWORDByReference lpdwResult = null;

    long result = lib.SendMessageTimeout(hnwdSend, msgSend, wParamSend, lParamSend, fuFlagsSend, uTimeoutSend,
            lpdwResult);
    System.out.println(result);
    System.out.println(lpdwResult);
    final Kernel32 kernel32 = Kernel32.INSTANCE;
    int lastError = kernel32.GetLastError();
    System.out.println(lastError);
    msgSend = 0x0101; // Release
    lib.SendMessageTimeout(hnwdSend, msgSend, wParamSend, lParamSend, fuFlagsSend, uTimeoutSend, lpdwResult);
Barjola
  • 11
  • 4
  • You may need to start a message pump in a separate thread (WaitMessage/GetMessage loop). – technomage Jan 14 '16 at 19:13
  • Thanks, I'll try that this weeking and see what happens. – Barjola Jan 15 '16 at 12:43
  • I think that I really need to user that wait/getmessage: while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) { ... } I will try other options to solve my problem anyway, thanks a lot, it was a good hint. – Barjola Jan 18 '16 at 10:51
  • After some more research I wasn't sure this is the good way, so I kept looking for differents options and found that: http://stackoverflow.com/questions/27407920/how-do-i-generate-keyboard-events-that-dont-have-key-code-in-java The Jade response works for me. – Barjola Jan 19 '16 at 11:26

0 Answers0