Simple SendInput example with Virtual Keys

2,791 views
Skip to first unread message

Ken F

unread,
Apr 2, 2012, 8:34:07 PM4/2/12
to Java Native Access
Hi, I am just trying to do a simple SendInput command to Notepad so
that is writes characters to the application. I have explained below
in code why I want to do this. The end of the internet has been
unable to supply me a clear and simple explanation. So, I pieced
together what I could.



package main;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.INPUT;
import com.sun.jna.platform.win32.WinUser.*;
import com.sun.jna.platform.win32.WinDef;

public class Main
{
public static GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
public static GraphicsDevice[] devices =
environment.getScreenDevices();
public static GraphicsDevice screen = devices[0];

public static final User32 lib = User32.INSTANCE;


/*
* I can use java robot to send keyboard input. This is a bit of a
problem since this is a desktop app.
* Each user can arrange the app in differnt locations, and screens,
and can adjust font size.
* I know I can resize and reposition the app using windows API, but
this is a bit annoying to some.
* This makes absolute screen positioning an issue. So, I want to
use SendInput to a Windows Handle App.
* Ironically this is a pre windows 95 app. I am using Notepad to
test the SendInput function.
* IDE Eclipse Indigo and also MyEclipse 10.
*
* Jna 3.40 from github twal
*
* OS Windows XP
* 32 bit
* 4G Ram
* Dual Core
*
*/

public static void main (String[] args)
{
HWND hwnd_Notepad = User32.INSTANCE.FindWindow(null, "Untitled -
Notepad");
User32.INSTANCE.SetForegroundWindow(hwnd_Notepad);
User32.INSTANCE.SetFocus(hwnd_Notepad);

INPUT in = new INPUT();
in.input.setType(KEYBDINPUT.class);
in.type = new DWORD(1);
//Lets say I want to send keyboard input of a character to the app
'a' or simulate the input with a virtual key
// below does not work
in.input.ki.wScan = 'a'; // error here
// how do I translate this to from a ascii to a keycode to send to
a window Handle?
in.input.ki.dwFlags = new
DWORD(WinUser.KEYBDINPUT.KEYEVENTF_SCANCODE);

INPUT[] inputs = (INPUT[])in.input.toArray(1);
int out = User32.SendInput(inputs.length, inputs,28); //error here

// do I now need to send a keyup/ down since I am using scancode?

}
}

Ken F

unread,
Apr 4, 2012, 9:27:30 AM4/4/12
to Java Native Access
Update:

I found java robot does fine and I do not need to send virtual
codes. Just make sure your windows Handle has focus. Then it is
just a matter of doing your robot.keypress . This brings me to me
to a new issue.

I have a program that is a child of another program. There is no
window text in the child progam ( setMenu null ?) Anywho, can you
suggest anyplace where I can find an implementation of
EnumChildWindows so that I can get the handle of a child that has no
window text? I have a sneaking suspicion that the child handle I want
is actually a grandchild.

Kamran Wali

unread,
Aug 20, 2013, 7:24:47 AM8/20/13
to jna-...@googlegroups.com
Hi Ken F.
I was having the same problem by sending input through SendInput(). I tried to use Robot but it looks like some softwares can't detect Robot. Is there any chance that you solved the problem for the SendInput() in JNA?

Thanks
Kamran Wali

Ken F

unread,
Aug 20, 2013, 7:20:01 PM8/20/13
to jna-...@googlegroups.com
Tim did post some pseudo code on a different thread  about this.   I do not have much experience in C so I had trouble implementing the solution.  But, Tim pointed out the Windows 7 OS should be blocking the send Input from another application.  For those two reasons, I went to Robot.  What type of system are you on?  I have had good luck with Robot on a few OS types.   My Windows 7 implementation works well with JNA to grab focus, re-size,  make a view  area ( think of it as a box within a box) ,  and click so to type. The downside is that the Robot is screen location dependent. (( Multiple Monitors can get a bit hectic along with non standard  resolutions))       One issue I noted was that if you have Windows Explorer open and the title bar has the same name as the window you want; the OS might grab Windows Explorer instead of the one you want. 
Reply all
Reply to author
Forward
0 new messages