Today is Saturday, 21st December 2024
Sponsored:

Methods of Sending Keys

Author: DarkKnightH20
Language: Visual Basic 6.0

This example I put together awhile ago. It is one of my worst in terms of guides, but hey, I put it together for reference purposes only. Don’t worry though, it’s easily understandable. Also, for those looking at this example, expecting to make a bot…I’m sorry, but this guide does not server that purpose. Many MMO’s have protection that blocks all methods listed in here or simply do not recognize them. What you will need is an alternate, low-level way to send the keys. You will find, however, that occasionally you WILL be able to make a bot despite this. Just don’t expect it to work on anything overly mainstream.


1. SendKeys method

Sendkeys String, Wait

As you can see, the above is a simple command. You can trigger it most usefully through hotkeys. If need be you can also use it in conjunction with the AppActivate function like so–


AppActivate "notepad"
Sendkeys "Pork!"


2. Keybd

Private Declare Sub keybd_event Lib “user32” (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Very useful. Here is a good example from VBAccelerator.com


Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" ( _
ByVal cChar As Byte) As Integer
Private Declare Function VkKeyScanW Lib "user32" ( _
ByVal cChar As Integer) As Integer

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Public Sub KeyDown(ByVal vKey As KeyCodeConstants)
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
End Sub

Public Sub KeyUp(ByVal vKey As KeyCodeConstants)
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub

Public Function KeyCode(ByVal sChar As String) As KeyCodeConstants
Dim bNt As Boolean
Dim iKeyCode As Integer
Dim b() As Byte
Dim iKey As Integer
Dim vKey As KeyCodeConstants
Dim iShift As ShiftConstants
' Determine if we have Unicode support or not:
bNt = ((GetVersion() And &H80000000) = 0)
' Get the keyboard scan code for the character:
If (bNt) Then
b = sChar
CopyMemory iKey, b(0), 2
iKeyCode = VkKeyScanW(iKey)
Else
b = StrConv(sChar, vbFromUnicode)
iKeyCode = VkKeyScan(b(0))
End If
KeyCode = (iKeyCode And &HFF&)
End Function


Use the code like so (via command button)–


Private Sub Command1_Click()
AppActivate "notepad"
KeyDown vbKeyL
KeyUp vbKeyL
KeyDown vbKeyO
KeyUp vbKeyO
KeyDown vbKeyL
KeyUp vbKeyL
KeyDown vbKeyReturn
End Sub


Simple. Very self explainatory, yah? Yah. Make sure to emulate they keys correctly by using a KeyUp AND KeyDown. Not a must, but depending on what you’re using it for, it can get you into a bit of trouble otherwise.


3. SendInput

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

This method uses SendInput. It also uses keybd to make life easier.

An example by “KPD-Team”–

Const VK_L = 76
Const VK_O = 79
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 Form_KeyPress(KeyAscii As Integer)

'Print the key on the form
Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
'KPD-Team 2000
'Clear the form

Me.Cls
'call the SendKey-function

SendKeyY VK_L
SendKeyY VK_O
SendKeyY VK_L
End Sub

Private Sub SendKeyY(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 release
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


Personally, I prefer the Keybd method in the earlier bit of this post in comparison to this one.


No Comments

  1. Comments  Geek Montage » Yet Another VB 6.0 Guide   |  Sunday, 16 August 2009 at 4:50 PM

    […] right folks, I added yet another Visual Basic 6.0 guide. This one is called Methods of Sending Keys and explores three different ways of doing so. Though some of these methods can be used to make […]

Leave a Reply





Sponsored

Affiliate Articles:

Amazon Deals

Top