Simulating User Actions

Keyboard Events

This module lets you simulate keyboard events. Many thanks to Rohit Kulshreshtha for this!

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

Public Sub KeyDown(KCC As KeyCodeConstants)
    keybd_event KCC, 0, 0, 0
End Sub

Public Sub KeyUp(KCC As KeyCodeConstants)
    keybd_event KCC, 0, KEYEVENTF_KEYUP, 0
End Sub

Public Sub KeyPress(KCC As KeyCodeConstants)
    KeyDown KCC
    KeyUp KCC
End Sub

Public Sub ShiftOnn()
    KeyDown vbKeyShift
End Sub

Public Sub ShiftOff()
    KeyUp vbKeyShift
End Sub

Public Sub CtrlOnn()
    KeyDown vbKeyControl
End Sub

Public Sub CtrlOff()
    KeyUp vbKeyControl
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous