VB Code:
  1. Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
  2. Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
  3.  
  4. Private Const VK_CAPITAL = &H14
  5. Private Const VK_NUMLOCK = &H90
  6. Private Const VK_SCROLL = &H91
  7.  
  8. ReDim KeyboardBuffer(256) As Byte
  9. GetKeyboardState KeyboardBuffer(0)
  10. 'This example show you how to press the Caps Lock button. if you want
  11. 'to press the Num Lock button, Replace all the following 3 'VK_CAPITAL'
  12. 'with 'VK_NUMLOCK' or VK_SCROLL as desired.
  13. If KeyboardBuffer(VK_CAPITAL) And 1 Then
  14. KeyboardBuffer(VK_CAPITAL) = 0
  15. Else
  16. KeyboardBuffer(VK_CAPITAL) = 1
  17. End If
  18. SetKeyboardState KeyboardBuffer(0)
  19.  
  20. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
  21.  
  22. 'To check if Num Lock is on Replace the 'vbKeyCapital' below with 'vbKeyNumlock'
  23.  
  24. Dim lngWhatState As Long
  25. lngWhatState = GetKeyState(vbKeyCapital)
  26. If lngWhatState = 1 Then
  27. MsgBox "The Caps Lock Is On"
  28. Else
  29. MsgBox "The Caps Lock Is Off"
  30. End If