Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controling num lock, caps lock and scroll lock

Status
Not open for further replies.

Pete222

Programmer
Dec 29, 2002
85
0
0
GB
I was wondering if anyone knew how to switch num lock, caps lock or/and scroll lock, on or off separately.

Pete.
My site: clix.to/F
 
In a Bas module

'API declarations
Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Function GetCapsLock() As Integer
'Returns the state of the CapsLock key
GetCapsLock = GetKeyState(vbKeyCapital)
End Function

Function GetNumLock() As Integer
'Returns the state of the NumLock key
GetNumLock = GetKeyState(vbKeyNumlock)
End Function

Function GetScrollLock() As Integer
'Returns the state of the ScrollLock key
GetScrollLock = GetKeyState(vbKeyScrollLock)
End Function

Sub SetCapsLock(Value As Boolean)
'Sets the state of the CapsLock key
Call SetKeyState(vbKeyCapital, Value)
End Sub

Sub SetNumLock(Value As Boolean)
'Sets the state of the NumLock key
Call SetKeyState(vbKeyNumlock, Value)
End Sub

Sub SetScrollLock(Value As Boolean)
'Sets the state of the ScrollLock key
Call SetKeyState(vbKeyScrollLock, Value)
End Sub

Private Sub SetKeyState(KeyName As Integer, KeyValue As Boolean)
'This code will retrieve the keyboard state, sets the particular
'key state passed to the 'KeyName' argument with the value passed
'to the 'KeyValue' argument, and then sets the entire keyboard
'state back to the state it was before with the new value of the
'key just changed
Dim Buffer(0 To 255) As Byte
GetKeyboardState Buffer(0)
Buffer(KeyName) = CByte(Abs(KeyValue))
SetKeyboardState Buffer(0)
End Sub

Then just call the desired procedure. Hope this helps.



If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
No, SendKeys can't. It is one of it's (many) failings.
 
Even though I prefer the API function as in Foada's example, there is also an old VB control available for this in your VS\Common\Tools\Controls folder, called KeySta32.Ocx which does the same.
 
Sorry im a bit new ('Taught myself' - Beginner to Intermediate level.)

Can you show how i would click 'CMD1' and Caps Lock would turn on...?

thanks for the help...I cant find 'KeySta32.Ocx' anywhere either. :-(

Pete.
My site: clix.to/F
 
keysta32.ocx should be in tools\controls directory under VB6 on your original installation disk, as long as you're not running the Learning Edition, which doesn't include it.

For your first question, foada's code just does it.


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top