Library code snippets

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

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Simulating Keyboard Events.

Leave a comment

Sign in or Join us (it's free).

 Rohit

Related discussion

Related podcasts

  • Visual Studio Talk Show (en français): Christian Beauclair

    Published 1 year ago, running time 0h51m

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss... de, il, christian, vb6

Want to stay in touch with what's going on? Follow us on twitter or Facebook!