AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

How to Activate CapsLock and NumLock from Code

Declarations

Add the following code into the declarations section of a module:

Public Const VK_CAPITAL = &H14

Public Type KeyboardBytes
     kbByte(0 To 255) As Byte
End Type

Public kbArray As KeyboardBytes

Public Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Long

Public Declare Function GetKeyboardState Lib "user32" _
(kbArray As KeyboardBytes) As Long

Public Declare Function SetKeyboardState Lib "user32" _
(kbArray As KeyboardBytes) As Long

Code

On a form, add a 3 command buttons (cmdToggle, cmdTurnOn, cmdTurnOff) and a label (Label1). Add the following code to the form:

Private Sub Form_Load()
If CapsLock() = 1 Then Label1 = "On" Else _
Label1 = "Off"
End Sub

Private Sub cmdToggle_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = _
IIf(kbArray.kbByte(VK_CAPITAL) = 1, 0, 1)
SetKeyboardState kbArray

Label1 = IIf(CapsLock() = 1, "On", "Off")
End Sub

Private Sub cmdTurnOn_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 1
SetKeyboardState kbArray

Label1 = IIf(CapsLock() = 1, "On", "Off")
End Sub

Private Sub cmdTurnOff_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 0
SetKeyboardState kbArray

Label1 = IIf(CapsLock() = 1, "On", "Off")
End Sub

Comments

The keyboard APIs for VB4-16 and VB3 do not support the byte data type.

By changing the Windows constant to Public Const VK_NUMLOCK = &H90, you can use the above to activate the NumLock key.

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/