VB中怎样编一个自动按键盘的软件~

参考了前辈们的文章,得到了sendinput的一个小实例,先学习了:

这个是真正的模拟按键,跟sendkeys完全不同哦~~~注意在按下form中的按钮后,迅速切换到自己的程序中测试。

'form中放一个command1和一个timer,为了方便演示,建议放一个text1在form中

Const VK_A = 65

Const KEYEVENTF_KEYUP = &H2

Const INPUT_MOUSE = 0

Const INPUT_KEYBOARD = 1

Const INPUT_HARDWARE = 2

Private Type MOUSEINPUT

dx As Long

dy As Long

mouseData As Long

dwFlags As Long

time As Long

dwExtraInfo As Long

End Type

Private Type KEYBDINPUT

wVk As Integer

wScan As Integer

dwFlags As Long

time As Long

dwExtraInfo As Long

End Type

Private Type HARDWAREINPUT

uMsg As Long

wParamL As Integer

wParamH As Integer

End Type

Private Type GENERALINPUT

dwType As Long

xi(0 To 23) As Byte

End Type

Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Sub SendKey(bKey As Byte)

Dim GInput(0 To 1) As GENERALINPUT

Dim KInput As KEYBDINPUT

KInput.wVk = bKey 'the key we're going to press

KInput.dwFlags = 0 'press the key

'copy the structure into the input array's buffer.

GInput(0).dwType = INPUT_KEYBOARD ' keyboard input

CopyMemory GInput(0).xi(0), KInput, Len(KInput)

'do the same as above, but for releasing the key

KInput.wVk = bKey ' the key we're going to realease

KInput.dwFlags = KEYEVENTF_KEYUP ' release the key

GInput(1).dwType = INPUT_KEYBOARD ' keyboard input

CopyMemory GInput(1).xi(0), KInput, Len(KInput)

'send the input now

Call SendInput(2, GInput(0), Len(GInput(0)))

End Sub

Private Sub Command1_Click()

Timer1.Enabled = Not (Timer1.Enabled)

End Sub

Private Sub Form_Load()

Timer1.Enabled = False

End Sub

Private Sub Timer1_Timer()

SendKey VK_A

End Sub