as the title describes: Is there a way to send simulated keystrokes to an inactive window by using JNA ( ,because Java is my strongest language)? Of course when there is an alternative language, which can achieve this goal, I'd go for that.
I read lots of stuff on the web, also besides JNA but with no succes for this goal.
Right now I am able to simulate keystrokes with sendInput() with JNA but thats not what want, because it effects the active window. You can read about that here: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendinput
My understanding is that you can use sendMessage() for that topic, but I cant get it working. https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendmessage
LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );
There are also SendMessageA and SendMessageW. Some say SendMessage is too old for some os, but I could not verify that.
Lets take Notepad as an example. The window title is 'new 2 - Notepad++'
Keydown: https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-keydown
Keyup: https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-keyup
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
LRESULT SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
}
public void winAPI() throws InterruptedException {
HWND handler = User32.INSTANCE.FindWindow(null, "new 2 - Notepad++");
// 0x0100 WM_KEYDOWN
User32.INSTANCE.SendMessage(handler, 0x0100, new WinDef.WPARAM(0x31), new WinDef.LPARAM(0)}
// recommended for dedection
Thread.sleep(200);
// 0x0101 WM_KEYUP
User32.INSTANCE.SendMessage(handler, 0x0101, new WinDef.WPARAM(0x31), new WinDef.LPARAM(0)}
}
I struggle with the right implementation of SendMessage(A?)(W?)() since its not implemented in JNA.
Also how do you create WPARAM and LPARAM? MSDN says there are message specific. So when pass WM_KEYDOWN or WM_KEYUP as message parameter:
WPARAM is the virual KeyCode: just an int?
LPARAM is a bytearray(?).
I guess it doesnt work because of wrong parameter datatypes of WPARAM and LPARAM.