Không hoạt động khi chạy phím cách bằng mã

lập trình


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

Những gì tôi đã thử:

Mình đang dùng vb để viết script.

Tôi muốn viết mã để điều khiển thanh dấu cách của trình duyệt chrome và Edge nhưng mã của tôi không hoạt động.

có thể giúp mình xem nhé, cảm ơn bạn.

Giải pháp 1

Ừm… bạn đã đọc SendInput làm gì rồi phải không? Nó cung cấp thông tin bàn phím và chuột vào hàng đợi thích hợp – có nghĩa là thông tin này được ứng dụng đang hoạt động trích xuất.
Vì bạn vừa nhấn nút chuột không phải là Chrome: đó là ứng dụng của bạn.

Bạn cần gửi chúng đến một cửa sổ cụ thể (thử FindWindow và SetForegroundWindow) rồi sử dụng SendKeys để gửi không gian tới Chrome.
Không có gì đảm bảo nó sẽ hoạt động, nhưng…

コメント

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