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);