No funciona cuando se ejecuta la barra espaciadora usando código

programación


VB.NET
Imports System.Runtime.InteropServices

 <dllimport("user32.dll", setlasterror:="True)">
 Private Shared Function SendInput(
     ByVal nInputs As UInteger,
     ByVal pInputs As INPUT(),
     ByVal cbSize As Integer
 ) As UInteger
 End Function

 ' Define the INPUT structure
 <structlayout(layoutkind.sequential)>
 Structure INPUT
     Public type As Integer
     Public ki As KEYBOARDINPUT
 End Structure

 ' Define the KEYBOARDINPUT structure
 <structlayout(layoutkind.sequential)>
 Structure KEYBOARDINPUT
     Public wVk As UShort
     Public wScan As UShort
     Public dwFlags As UInteger
     Public time As UInteger
     Public dwExtraInfo As IntPtr
 End Structure

 ' Constants for input type and key events
 Const INPUT_KEYBOARD As Integer = 1
 Const KEYEVENTF_KEYUP As UInteger = &H2

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     ' Simulate pressing the space bar using SendInput
     SendSpaceBar()
 End Sub

 Private Sub SendSpaceBar()
     Dim input As New INPUT()
     input.type = INPUT_KEYBOARD
     input.ki.wVk = &H20 ' VK_SPACE code for space bar

     ' Press the space bar
     SendInput(1, {input}, Marshal.SizeOf(GetType(INPUT)))

     ' Release the space bar
     input.ki.dwFlags = KEYEVENTF_KEYUP
     SendInput(1, {input}, Marshal.SizeOf(GetType(INPUT)))
 End Sub

Lo que he probado:

Estoy usando vb para escribir el guión.

Quiero escribir el código para controlar Chrome y la barra espaciadora del navegador Edge, pero mi código no funciona.

me pueden ayudar a echarle un vistazo gracias.

Solución 1

Erm… leíste lo que hace SendInput, ¿verdad? Introduce información del teclado y el mouse en las colas apropiadas, lo que significa que la aplicación activa las extrae.
Que, como acabas de presionar un botón del mouse, no es Chrome: es tu aplicación.

Debe enviarlos a una ventana específica (pruebe FindWindow y SetForegroundWindow) y luego use SendKeys para enviar el espacio a Chrome.
No hay garantías de que funcione, pero…

コメント

タイトルとURLをコピーしました