Ken F
unread,Apr 2, 2012, 8:34:07 PM4/2/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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?
}
}