我已将我的项目代码压缩到下面的示例中以显示我的问题。
示例/代码段引用了使用here和here的概念。 控制台输出显示发送的4个事件;所以我希望记事本中的输出是" 12",但我只看到" 1"在记事本中。有人能指出我正确的方向吗?该jar是在Windows 10 x64上构建和运行的。
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WTypes.BSTR;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.User32;
public class App {
public static void main( String[] args )
{
int KEYEVENT_SCANCODE = 0x0008;
int KEYEVENT_UP = 0x0002;
WinUser.INPUT[] inputArray = (WinUser.INPUT[]) new WinUser.INPUT().toArray(4);
inputArray[0].type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
inputArray[0].input.setType("ki");
inputArray[0].input.ki.time = new WinDef.DWORD(0);
inputArray[0].input.ki.wVk = new WinDef.WORD(0); //using scancodes instead
inputArray[0].input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
inputArray[0].input.ki.dwFlags = new WinDef.DWORD(KEYEVENT_SCANCODE);
inputArray[0].input.ki.wScan = new WinDef.WORD(0x01);
inputArray[1].type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
inputArray[1].input.setType("ki");
inputArray[1].input.ki.time = new WinDef.DWORD(0);
inputArray[1].input.ki.wVk = new WinDef.WORD(0); //using scancodes instead
inputArray[1].input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
inputArray[1].input.ki.dwFlags = new WinDef.DWORD(KEYEVENT_SCANCODE | KEYEVENT_UP);
inputArray[1].input.ki.wScan = new WinDef.WORD(0x01);
inputArray[2].type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
inputArray[2].input.setType("ki");
inputArray[2].input.ki.time = new WinDef.DWORD(0);
inputArray[2].input.ki.wVk = new WinDef.WORD(0); //using scancodes instead
inputArray[2].input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
inputArray[2].input.ki.dwFlags = new WinDef.DWORD(KEYEVENT_SCANCODE);
inputArray[2].input.ki.wScan = new WinDef.WORD(0x02);
inputArray[3].type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
inputArray[3].input.setType("ki");
inputArray[3].input.ki.time = new WinDef.DWORD(0);
inputArray[3].input.ki.wVk = new WinDef.WORD(0); //using scancodes instead
inputArray[3].input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
inputArray[3].input.ki.dwFlags = new WinDef.DWORD(KEYEVENT_SCANCODE | KEYEVENT_UP);
inputArray[3].input.ki.wScan = new WinDef.WORD(0x02);
System.out.println("Make sure notepad is in focus...");
try { Thread.sleep(7000); } catch (Throwable ex) { System.out.println("Interrupted"); }
WinDef.DWORD sentInputs = User32.INSTANCE.SendInput(
new WinDef.DWORD(inputArray.length),
inputArray,
inputArray[0].size()
);
System.out.println("Sent Input Count: " + sentInputs.intValue());
}
}
控制台输出:
E:\ SKGDE \ my-app \ target> java -jar my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
确保记事本处于焦点...
已发送输入计数:4
E:\ SKGDE \我的应用内\靶>
答案 0 :(得分:0)
记事本仅显示“1”而非“12”,因为您发送的扫描代码错误。在这里,您首先发送 ESC KEY ( 0x01 )的扫描码,然后发送 1 KEY的扫描码 ( 0x02 )。
您应首先发送 1 KEY ( 0x02 )的扫描码,然后 2 KEY ( 0x03 )
使用ScanCode.java,这样您就不必记住扫描码。
public interface ScanCode {
public static final int DIK_ESCAPE =0x01;
public static final int DIK_1 =0x02;
public static final int DIK_2 =0x03;
public static final int DIK_3 =0x04;
public static final int DIK_4 =0x05;
public static final int DIK_5 =0x06;
public static final int DIK_6 =0x07;
public static final int DIK_7 =0x08;
public static final int DIK_8 =0x09;
public static final int DIK_9 =0x0A;
public static final int DIK_0 =0x0B;
public static final int DIK_MINUS =0x0C; /* - on main keyboard */
public static final int DIK_EQUALS =0x0D;
public static final int DIK_BACK =0x0E; /* backspace */
public static final int DIK_TAB =0x0F;
public static final int DIK_Q =0x10;
public static final int DIK_W =0x11;
public static final int DIK_E =0x12;
public static final int DIK_R =0x13;
public static final int DIK_T =0x14;
public static final int DIK_Y =0x15;
public static final int DIK_U =0x16;
public static final int DIK_I =0x17;
public static final int DIK_O =0x18;
public static final int DIK_P =0x19;
public static final int DIK_LBRACKET =0x1A;
public static final int DIK_RBRACKET =0x1B;
public static final int DIK_RETURN =0x1C; /* Enter on main keyboard */
public static final int DIK_LCONTROL =0x1D;
public static final int DIK_A =0x1E;
public static final int DIK_S =0x1F;
public static final int DIK_D =0x20;
public static final int DIK_F =0x21;
public static final int DIK_G =0x22;
public static final int DIK_H =0x23;
public static final int DIK_J =0x24;
public static final int DIK_K =0x25;
public static final int DIK_L =0x26;
public static final int DIK_SEMICOLON =0x27;
public static final int DIK_APOSTROPHE =0x28;
public static final int DIK_GRAVE =0x29; /* accent grave */
public static final int DIK_LSHIFT =0x2A;
public static final int DIK_BACKSLASH =0x2B;
public static final int DIK_Z =0x2C;
public static final int DIK_X =0x2D;
public static final int DIK_C =0x2E;
public static final int DIK_V =0x2F;
public static final int DIK_B =0x30;
public static final int DIK_N =0x31;
public static final int DIK_M =0x32;
public static final int DIK_COMMA =0x33;
public static final int DIK_PERIOD =0x34; /* . on main keyboard */
public static final int DIK_SLASH =0x35; /* / on main keyboard */
public static final int DIK_RSHIFT =0x36;
public static final int DIK_MULTIPLY =0x37; /* * on numeric keypad */
public static final int DIK_LMENU =0x38; /* left Alt */
public static final int DIK_SPACE =0x39;
public static final int DIK_CAPITAL =0x3A;
public static final int DIK_F1 =0x3B;
public static final int DIK_F2 =0x3C;
public static final int DIK_F3 =0x3D;
public static final int DIK_F4 =0x3E;
public static final int DIK_F5 =0x3F;
public static final int DIK_F6 =0x40;
public static final int DIK_F7 =0x41;
public static final int DIK_F8 =0x42;
public static final int DIK_F9 =0x43;
public static final int DIK_F10 =0x44;
public static final int DIK_NUMLOCK =0x45;
public static final int DIK_SCROLL =0x46; /* Scroll Lock */
public static final int DIK_NUMPAD7 =0x47;
public static final int DIK_NUMPAD8 =0x48;
public static final int DIK_NUMPAD9 =0x49;
public static final int DIK_SUBTRACT =0x4A; /* - on numeric keypad */
public static final int DIK_NUMPAD4 =0x4B;
public static final int DIK_NUMPAD5 =0x4C;
public static final int DIK_NUMPAD6 =0x4D;
public static final int DIK_ADD =0x4E; /* + on numeric keypad */
public static final int DIK_NUMPAD1 =0x4F;
public static final int DIK_NUMPAD2 =0x50;
public static final int DIK_NUMPAD3 =0x51;
public static final int DIK_NUMPAD0 =0x52;
public static final int DIK_DECIMAL =0x53; /* . on numeric keypad */
public static final int DIK_OEM_102 =0x56; /* <> or \| on RT 102-key keyboard (Non-U.S.) */
public static final int DIK_F11 =0x57;
public static final int DIK_F12 =0x58;
public static final int DIK_F13 =0x64; /* (NEC PC98) */
public static final int DIK_F14 =0x65; /* (NEC PC98) */
public static final int DIK_F15 =0x66; /* (NEC PC98) */
public static final int DIK_KANA =0x70; /* (Japanese keyboard) */
public static final int DIK_ABNT_C1 =0x73; /* /? on Brazilian keyboard */
public static final int DIK_CONVERT =0x79; /* (Japanese keyboard) */
public static final int DIK_NOCONVERT =0x7B; /* (Japanese keyboard) */
public static final int DIK_YEN =0x7D; /* (Japanese keyboard) */
public static final int DIK_ABNT_C2 =0x7E; /* Numpad . on Brazilian keyboard */
public static final int DIK_NUMPADEQUALS =0x8D; /* = on numeric keypad (NEC PC98) */
public static final int DIK_PREVTRACK =0x90; /* Previous Track (DIK_CIRCUMFLEX on Japanese keyboard) */
public static final int DIK_AT =0x91; /* (NEC PC98) */
public static final int DIK_COLON =0x92; /* (NEC PC98) */
public static final int DIK_UNDERLINE =0x93; /* (NEC PC98) */
public static final int DIK_KANJI =0x94; /* (Japanese keyboard) */
public static final int DIK_STOP =0x95; /* (NEC PC98) */
public static final int DIK_AX =0x96; /* (Japan AX) */
public static final int DIK_UNLABELED =0x97; /* (J3100) */
public static final int DIK_NEXTTRACK =0x99; /* Next Track */
public static final int DIK_NUMPADENTER =0x9C; /* Enter on numeric keypad */
public static final int DIK_RCONTROL =0x9D;
public static final int DIK_MUTE =0xA0; /* Mute */
public static final int DIK_CALCULATOR =0xA1; /* Calculator */
public static final int DIK_PLAYPAUSE =0xA2; /* Play / Pause */
public static final int DIK_MEDIASTOP =0xA4; /* Media Stop */
public static final int DIK_VOLUMEDOWN =0xAE; /* Volume - */
public static final int DIK_VOLUMEUP =0xB0; /* Volume + */
public static final int DIK_WEBHOME =0xB2; /* Web home */
public static final int DIK_NUMPADCOMMA =0xB3; /* , on numeric keypad (NEC PC98) */
public static final int DIK_DIVIDE =0xB5; /* / on numeric keypad */
public static final int DIK_SYSRQ =0xB7;
public static final int DIK_RMENU =0xB8; /* right Alt */
public static final int DIK_PAUSE =0xC5; /* Pause */
public static final int DIK_HOME =0xC7; /* Home on arrow keypad */
public static final int DIK_UP =0xC8; /* UpArrow on arrow keypad */
public static final int DIK_PRIOR =0xC9; /* PgUp on arrow keypad */
public static final int DIK_LEFT =0xCB; /* LeftArrow on arrow keypad */
public static final int DIK_RIGHT =0xCD; /* RightArrow on arrow keypad */
public static final int DIK_END =0xCF; /* End on arrow keypad */
public static final int DIK_DOWN =0xD0; /* DownArrow on arrow keypad */
public static final int DIK_NEXT =0xD1; /* PgDn on arrow keypad */
public static final int DIK_INSERT =0xD2; /* Insert on arrow keypad */
public static final int DIK_DELETE =0xD3; /* Delete on arrow keypad */
public static final int DIK_LWIN =0xDB; /* Left Windows key */
public static final int DIK_RWIN =0xDC; /* Right Windows key */
public static final int DIK_APPS =0xDD; /* AppMenu key */
public static final int DIK_POWER =0xDE; /* System Power */
public static final int DIK_SLEEP =0xDF; /* System Sleep */
public static final int DIK_WAKE =0xE3; /* System Wake */
public static final int DIK_WEBSEARCH =0xE5; /* Web Search */
public static final int DIK_WEBFAVORITES =0xE6; /* Web Favorites */
public static final int DIK_WEBREFRESH =0xE7; /* Web Refresh */
public static final int DIK_WEBSTOP =0xE8; /* Web Stop */
public static final int DIK_WEBFORWARD =0xE9; /* Web Forward */
public static final int DIK_WEBBACK =0xEA; /* Web Back */
public static final int DIK_MYCOMPUTER =0xEB; /* My Computer */
public static final int DIK_MAIL =0xEC; /* Mail */
public static final int DIK_MEDIASELECT =0xED; /* Media Select */
}