Hello and a happy 2016
I decided to start the new year by trying out JNA for a project. I have a working application that can bring-to-foreground and focus a window based on the name of the window.
Now i have used quite a bit of time trying to make sendInput work, but not with any real success i'm afraid. I hope you can see what i am doing wrong.
The entry for the code below is
bringToFrontandFocus("Untitled - Notepad");
Thread.sleep(500);
typeMessage("Yay for JNA!");
Before this, the to-forground and focus methods are called successfully (I can manually write in the notepad i target after running my application, by typing on the keyboard)
But no matter what i try to write via the application, no keyboard events seems to be sent to windows.
Below is my example with thoughts and extras written as comments.:
protected void typeMessage(String message) {
for (char s : message.toCharArray()) {
int charUnicodeDecimalValue = s; //Get the unicode decimal
keyPress(charUnicodeDecimalValue); //also tested with hardcoded values such as 63 or 65 ('?' and 'A' i believe)
}
}
protected void keyPress(int unicodechar) {
logger.info("Typing: "+(char)unicodechar);
INPUT ip;
ip = new INPUT();
ip.type = new DWORD(INPUT.INPUT_KEYBOARD);
ip.input.ki.time = new DWORD(0); //The timestamp
ip.input.ki.dwFlags = new DWORD(WinUser.KEYBDINPUT.KEYEVENTF_UNICODE); //I am handing you a unicode character
ip.input.ki.wScan = new WORD(unicodechar); //The unicode code in decimal (right?)
ip.input.ki.wVk = new WORD(0); //Virtual key code, i am setting this to 0 because of the unicode flag in dwFlags
ip.input.ki.dwExtraInfo = new ULONG_PTR(0); //I have no idea in hell of what this does :)
User32.INSTANCE.SendInput(new DWORD(1), new INPUT[]{ip}, ip.size()); //I am setting this size of the array, the array itself, and the size of the ip variable.
}
I assumed that the size might be off, so i have tried giving it the size of the ip.input, and ip.input.ki as well, to no avail.
The result of running the code is: Nothing.
The notepad application is brought to the foreground, and put in focus. When my test terminates i can write directly in notepad without any prior actions.
But nothing is ever written to the notepad from the application. I don't recieve any errors in the log, and no exceptions, its as if the like sendInput command is never executed. (It is though, i ran it thorugh debug as well, and the info message is written to the log)