[英]Is possible to send VM_CHAR message to a console program launch by ProcessBuilder? (windows platform)
我想將VK_ENTER
模擬為控制台程序。
通過cmd.exe
手動啟動程序時,我可以成功發布VK_ENTER
消息。
找不到由ProcessBuilder
啟動的控制台程序的hwnd。
@Test
public void testGetPid(){
ProcessBuilder pb = new ProcessBuilder("test.exe"); // not work
// ProcessBuilder pb = new ProcessBuilder("notepad.exe"); // work
Process p = null;
try {
p = pb.start();
//"handle"
System.out.println(p);
Field f = p.getClass().getDeclaredField( "handle");
f.setAccessible( true);
long procHandle = f.getLong( p);
System.out.println("Handle: " + procHandle);
//Kernel32.INSTANCE.
HANDLE handle = new HANDLE();
handle.setPointer(Pointer.createConstant(procHandle));
final int pid = Kernel32.INSTANCE.GetProcessId(handle);
System.out.println("Pid: " + pid);
ThreadUtil.sleep(5*1000);
sendKeyToProcess(pid);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ThreadUtil.sleep(60*1000);
if( p != null )
p.destroy();
}
}
private void sendKeyToProcess(final int pid) {
final User32 user32Lib = User32.INSTANCE;
final int WM_KEYDOWN = 0x0100;
final int WM_KEYUP = 0x0101;
final int WM_CHAR = 0x0102;
user32Lib.EnumWindows(new WinUser.WNDENUMPROC() {
@Override
public boolean callback(HWND paramHWND, Pointer paramPointer) {
IntByReference intRef = new IntByReference();
user32Lib.GetWindowThreadProcessId(paramHWND, intRef);
if( pid == intRef.getValue() ){
// final WPARAM wPARAM = new WPARAM(13); // enter
final WPARAM wPARAM = new WPARAM(48); // '0'
final LPARAM lPARAM = new LPARAM(0);
System.out.println("Match hwnd: " + paramHWND.toString());
user32Lib.PostMessage(paramHWND,
WM_KEYDOWN, wPARAM, lPARAM);
User32.INSTANCE.EnumChildWindows(paramHWND,
new WinUser.WNDENUMPROC() {
@Override
public boolean callback(HWND childParamHWND,
Pointer paramPointer) {
System.out.println("send");
user32Lib.PostMessage(childParamHWND,
WM_KEYDOWN, wPARAM, lPARAM);
return true;
}
}, null);
//return false;
}
return true;
}
}, null);
}
它對我有用:(所有JNA解決方案)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.