Simulating Keyboard Events

Here's a neat bit of code that lets you simulate keyboard events, including Shift and Ctrl actions.

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

 Rohit

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.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie