Tidak berfungsi saat menjalankan spasi menggunakan kode

pemrograman


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

Apa yang saya coba:

Saya menggunakan vb untuk menulis skrip.

Saya ingin menulis kode untuk mengontrol bilah spasi chrome dan edge broswer tetapi kode saya tidak berfungsi.

dapat membantu saya melihatnya, terima kasih.

Solusi 1

Erm … Anda sudah membaca apa yang dilakukan SendInput, kan? Ini memasukkan informasi keyboard dan mouse ke dalam antrian yang sesuai – yang berarti diekstraksi oleh aplikasi yang aktif.
Yang mana karena Anda baru saja menekan tombol mouse bukanlah Chrome: melainkan aplikasi Anda.

Anda perlu mengirimkannya ke jendela tertentu (coba FindWindow dan SetForegroundWindow) lalu gunakan SendKeys untuk mengirim ruang ke Chrome.
Tidak ada jaminan itu akan berhasil, tapi…

コメント

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