Visual Basic Screen & System Metrics

keybd_event: Manipulating the Desktop
     
Posted:   Tuesday October 7, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SetKeyboardState: Activating CapsLock and NumLock on Win9x
keybd_event: Activating CapsLock, NumLock, ScrollLock and PrintScreen (NT or later)
     
 Prerequisites
None.

By using calls to the keybd_event API, an application can perform tasks as though the user had clicked on the start menu button or right-clicked the taskbar.

This demo shows how to launch Explorer, Find Files, and the Run Dialog, open the Start Menu, Minimize all windows, and launch Windows Help, all using the keybd_event API.

 BAS Module Code
None.

 Form Code
Add the following code to the form containing a control array of seven command buttons (Command1(0) - Command1(6)):

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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 VK_LWIN = &H5B
   Private Const KEYEVENTF_KEYUP = &H2
   Private Const VK_APPS = &H5D


Private Sub Command1_Click(Index As Integer)

   Dim VK_ACTION As Long

   Select Case Index
      Case 0: '&H45 is the hex character code
              'for the letter 'E' (ascii 69)
               VK_ACTION = &H45
      
      Case 1: '&H46 is the hex character code
              'for the letter 'F' (ascii 70)
               VK_ACTION = &H46
      
      Case 2: '&H4D is the hex character code
              'for the letter 'M' (ascii 77)
               VK_ACTION = &H4D
               
      Case 3: '&H52 is the hex character code
              'for the letter 'R' (ascii 82)
               VK_ACTION = &H52

      Case 4: '&H5B is the hex character code
              'for the start menu button
               VK_ACTION = &H5B
      
      Case 5: '&H5E is the hex character code
              'for the caret chr (ascii 94)
               VK_ACTION = &H5E

      Case 6: '&H70 is the hex character code
              'for the caret chr (ascii 112)
               VK_ACTION = &H70
      
   End Select
   
   Call keybd_event(VK_LWIN, 0, 0, 0)
   Call keybd_event(VK_ACTION, 0, 0, 0)
   Call keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)

End Sub
 Comments
That's it. Run the app and press the buttons to perform the indicated task.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter