使用代码运行空格键时不起作用

[ad_1]

网络
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

我尝试过的:

我正在使用 vb 来编写脚本。

我想编写代码来控制 chrome 和 Edge broswer 空格键,但我的代码不起作用。

可以帮我看看谢谢。

解决方案1

呃……你确实读过 SendInput 的作用,对吧? 它将键盘和鼠标信息提供到适当的队列中 – 这意味着这些信息由活动应用程序提取。
因为您只是按下了鼠标按钮,所以它不是 Chrome:它是您的应用程序。

您需要将它们发送到特定窗口(尝试 FindWindow 和 SetForegroundWindow),然后使用 SendKeys 将空间发送到 Chrome。
不保证它会起作用,但是……

[ad_2]

コメント

标题和URL已复制