我正在使用JNA将输入发送到虚幻的引擎游戏。我想键入" G"和" Numpad9"每5秒进行一次游戏,但出于某种原因," Numpad9"虽然" G"效果很好,所以这些键之间必须有区别。有谁能告诉我我能做些什么让它起作用?
编辑:游戏接收输入时不一定要有焦点!
如果有人需要,请使用以下代码:
public class DDAutostartWave
{
public static void main(String... args)
{
DDConnection.findRightHandle();
while(true)
{
try
{
Thread.sleep(5000);
} catch(InterruptedException e)
{
e.printStackTrace();
}
DDConnection.typeKey(KeyEvent.VK_G);
DDConnection.typeKey(KeyEvent.VK_NUMPAD9);
DDConnection.typeKey(KeyEvent.VK_NUMPAD9);
}
}
}
public class DDConnection
{
private static HWND rightWindowHandle;
private static User32 USER = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
public static void typeKey(int key)
{
try
{
pressKey(key);
Thread.sleep(3);
releaseKey(key);
Thread.sleep(3);
} catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void pressKey(int key)
{
WinDef.WPARAM wparam = new WinDef.WPARAM(key);
WinDef.LPARAM lparam = new WinDef.LPARAM(1 + (USER.MapVirtualKey(key, 0) << 16));
USER.SendMessageA(rightWindowHandle, 0x0100, wparam, lparam);
}
public static void releaseKey(int key)
{
WinDef.WPARAM wparam = new WinDef.WPARAM(key);
WinDef.LPARAM lparam = new WinDef.LPARAM(1 + (USER.MapVirtualKey(key, 0) << 16) + (1 << 30) + (1 << 31));
USER.SendMessageA(rightWindowHandle, 0x0101, wparam, lparam);
}
public static boolean findRightHandle()
{
rightWindowHandle = USER.FindWindow(null, "Dungeon Defenders");
if(rightWindowHandle == null) return false;
System.out.println(rightWindowHandle);
return true;
}
}
interface User32 extends com.sun.jna.platform.win32.User32
{
int MapVirtualKey(int ucode, int uMapType);
void SendMessageA(HWND handle, int message, WinDef.WPARAM wparam, WinDef.LPARAM lparam);
}