symulowanie zdarzenia naciśnięcia klawisza przy użyciu SendInput z C #
Zgodnie z radą z tego wątku:rozróżniać klawisze Real od wirtualnego
Próbuję stworzyć program, który będzie wysyłał zdarzenia klawiaturowe za pomocąSendInput () metoda.
Problem polega jednak na tym, że kiedy próbuję symulować zdarzenie kluczowe - nic się nie dzieje. Na razie to mój kod:
<code> [DllImport("user32.dll")] static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize); [StructLayout(LayoutKind.Sequential)] struct KEYBDINPUT { public short wScan; public int dwFlags; public int time; public IntPtr dwExtraInfo; } [StructLayout(LayoutKind.Explicit)] struct INPUT { [FieldOffset(0)] public int type; [FieldOffset(8)] public KEYBDINPUT ki; //x64 - 8, x32 - 4 } const int KEYEVENTF_DOWN = 0; //key UP const int KEYEVENTF_EXTENDEDKEY = 0x0001; const int KEYEVENTF_KEYUP = 0x0002; //key UP const int KEYEVENTF_UNICODE = 0x0004; const int KEYEVENTF_SCANCODE = 0x0008; // scancode public void Send_Key(short Keycode) { INPUT[] InputData = new INPUT[1]; InputData[0].type = 1; InputData[0].ki.wScan = Keycode; InputData[0].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE; InputData[0].ki.time = 0; InputData[0].ki.dwExtraInfo = IntPtr.Zero; SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT))); } </code>
Aby rozszyfrować skany, pobrałem program, który został zasugerowany w tym wątku:
https://superuser.com/questions/293609/windows-7-tool-to-capture-keyboard-scan-codes
zgodnie z tym programem - kod „a” dla mojej klawiatury to „65”
Mój program ma wywołać zdarzenie naciśnięcia klawisza, gdy tekst pola tekstowego zostanie zmieniony, więc jeśli wprowadzimy „q” - tekst powinien zmienić się na „q” plus wynik send_key (), tj. „Qa”:
<code> private void textBox2_TextChanged(object sender, EventArgs e) { Send_Key(0x0065); // nothing happens (0x65 also fails) } </code>
Co ja robię źle? W przyszłości zmienię ten kod, abym mógł określić czas przytrzymania klawisza (między zdarzeniami DOWN i UP). ale na razie do testowania symuluję tylko zdarzenie keyUP.
EDYTOWAĆ: Hans, zgodnie z twoją radą tutaj jest edycja - niestety nie działa.
<code> public void Send_Key(short Keycode) { INPUT[] InputData = new INPUT[1]; InputData[0].type = 1; InputData[0].ki.wScan = Keycode; InputData[0].ki.dwFlags = KEYEVENTF_DOWN; InputData[0].ki.time = 0; InputData[0].ki.dwExtraInfo = IntPtr.Zero; uint intReturn = SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT))); if (intReturn == 0) //!=1 { throw new Exception("Could not send keyDOWN: " + Keycode); } INPUT[] InputData2 = new INPUT[1]; InputData2[0].type = 1; InputData2[0].ki.wScan = Keycode; InputData2[0].mi.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE; InputData2[0].ki.time = 0; InputData2[0].ki.dwExtraInfo = IntPtr.Zero; uint intReturn2 = SendInput(1, InputData2, Marshal.SizeOf(typeof(INPUT))); if (intReturn2 == 0) //!=1 { throw new Exception("Could not send keyUP: " + Keycode); } } </code>
bez błędów .. i bez wyjścia.