Visual Basic System Services
Activating CapsLock, NumLock, ScrollLock and PrintScreen on NT-Based Systems
     
Posted:   Friday July 10, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   Windows NT4, Windows 2000, Windows XP
Author:   VBnet - Randy Birch, Stoil Marinov
     

Related:  

SetKeyboardState: Activating CapsLock and NumLock on Win9x
BitBlt: Mimicking the PrintScreen Function
BitBlt: Mimicking PrintScreen to Create a 'PrintForm'
OleCreatePictureIndirect: Mimicking PrintScreen Using OLE
     
 Prerequisites
VB version supporting the Byte data type, WinNT4/Win2000.

While the Get/SetKeyboardState APIs, when used on a WinNT system, will toggle the *state* of capslock or numlock, unlike Win9x, the keyboard lights remain unaffected by the call. This code, using the keybd_event API, provides the same functionality as the SetKeyboardState version, and in addition toggles the keyboard state lights. It is untested in Win9x (may even work there!), but works perfectly on NT.

Unlike the other examples listed under related topics, using the keybd_event API for PrintScreen causes the screen to go to the clipboard, where this demo then retrieves that bitmap into a image control.

Thanks go out to Stoil Marinov for posting the NT *lock method in the msnews groups.

 BAS Module Code
None.

 Form Code
On a form, add a five command buttons in a control array (Command(0) - Command(4)) and an Image control (Image1). as shown, the image control's Stretch property is True. Add the following code to the form:

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 Declare Function MapVirtualKey Lib "user32" _
   Alias "MapVirtualKeyA" _
  (ByVal wCode As Long, ByVal wMapType As Long) As Long
         
Private Declare Function Sleep Lib "kernel32" _
  (ByVal dwmilliseconds As Long) As Long

Private Const KEYEVENTF_KEYUP = &H2
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const VK_SCROLL = &H91


Private Sub Command1_Click(Index As Integer)

   Select Case Index
      Case 0: NTToggleKeyCaps
      Case 1: NTToggleKeyNumlock
      Case 2: NTToggleKeyScroll
      Case 3: NTToggleKeyPrintScreen
             
             'because Windows may still be drawing to
             'the clipboard when GetData or Clear is
             'called, a 521 - "Can't open clipboard'
             'error may be generated. The error handler
             'pauses the app for 1/2 second while
             'Windows finishes, then tries again.
              DoEvents
              On Local Error GoTo command_error
              Image1.Picture = Clipboard.GetData(vbCFBitmap)
              Clipboard.Clear
              On Local Error GoTo 0
              
      Case Else
          Unload Me
          Exit Sub
   End Select

  'let the user test the change
   Text1.SetFocus
   
   Exit Sub
   
command_error:

   Call Sleep(250)
   Resume Next
   
End Sub


Private Sub NTToggleKeyPrintScreen()

    Call keybd_event(vbKeySnapshot, _
                     MapVirtualKey(vbKeySnapshot, 0), _
                     KEYEVENTF_EXTENDEDKEY Or 0, 0)
                
    Call keybd_event(vbKeySnapshot, MapVirtualKey(vbKeySnapshot, 0), _
                     KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)

End Sub


Private Sub NTToggleKeyNumlock()

    Call keybd_event(vbKeyNumlock, _
                     MapVirtualKey(vbKeyNumlock, 0), _
                     KEYEVENTF_EXTENDEDKEY Or 0, 0)
                
    Call keybd_event(vbKeyNumlock, MapVirtualKey(vbKeyNumlock, 0), _
                     KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
End Sub


Private Sub NTToggleKeyCaps()

    Call keybd_event(vbKeyCapital, _
                     MapVirtualKey(vbKeyCapital, 0), _
                     KEYEVENTF_EXTENDEDKEY Or 0, 0)
                
    Call keybd_event(vbKeyCapital, MapVirtualKey(vbKeyCapital, 0), _
                     KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
End Sub


Private Sub NTToggleKeyScroll()

    Call keybd_event(VK_SCROLL, _
                     MapVirtualKey(VK_SCROLL, 0), _
                     KEYEVENTF_EXTENDEDKEY Or 0, 0)
                
    Call keybd_event(VK_SCROLL, MapVirtualKey(VK_SCROLL, 0), _
                     KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
End Sub


Private Sub Text1_GotFocus()

   With Text1
      .SelStart = 0
      .SelLength = Len(.Text)
   End With
   
End Sub
 Comments

 
 

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