Click to See Complete Forum and Search --> : detecting the keyboard scroll lock state


vb.elmar
October 19th, 2001, 02:53 PM
Hello
i need a function to detect the "scroll lock state".

I want to detect the state of the green scroll-lock
keyboard LED (OFF or ON).

The code below works, but when starting another
application like notepad, the scroll lock state no more
is displayed correctly by vb.

Only when closing notepad (i use the GetKeyboardState api)
the code works.


Using another api (GetAsyncKeyState)
i could detect the scroll lock state,
when notepad is started, but i only can
see that the key is pressed at this moment.
I can not see the scroll lock state
(is the scroll LED
on or of at this time?)

Can i detect the state or
the state of the scroll lock LED, when
another application is running ?

================================
needs 1 timer on a form
================================
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long

Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Private Sub Form_Load()
Timer1.Interval = 50
End Sub
Private Sub Timer1_Timer()
Dim State As Boolean
Dim Keys(0 To 255) As Byte

Call GetKeyboardState(Keys(0))
State = Keys(VK_SCROLL)
Caption = State & " " & Rnd
End Sub

Iouri
October 19th, 2001, 03:33 PM
'module
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Public Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long


Function GetCapslock() As Boolean
' Return or set the Capslock toggle.
GetCapslock = CBool(GetKeyState(vbKeyCapital) And 1)

End Function
Function GetNumlock() As Boolean
' Return or set the Numlock toggle.
GetNumlock = CBool(GetKeyState(vbKeyNumlock) And 1)
End Function


Function GetScrollLock() As Boolean
' Return or set the ScrollLock toggle.
GetScrollLock = CBool(GetKeyState(vbKeyScrollLock) And 1)
End Function


Sub SetCapslock(value As Boolean)
' Return or set the Capslock toggle.
Call SetKeyState(vbKeyCapital, value)
End Sub


Sub SetNumlock(value As Boolean)
' Return or set the Numlock toggle.
Call SetKeyState(vbKeyNumlock, value)
End Sub

Sub setscrolllock(value As Boolean)
' Return or set the ScrollLock toggle.
Call SetKeyState(vbKeyScrollLock, value)
End Sub


Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)

Dim abytBuffer(0 To 255) As Byte
GetKeyboardState abytBuffer(0)
abytBuffer(intKey) = CByte(Abs(fTurnOn))
SetKeyboardState abytBuffer(0)
End Sub


'form
Call GetCapslock
If GetCapslock = True Then
SetCapslock (False)
Else
Exit Sub
End If

Call GetCapslock
If GetCapslock = False Then
SetCapslock (True)
Else
Exit Sub
End If

Call GetNumlock
If GetNumlock = True Then
SetNumlock (False)
Else
Exit Sub
End If

Call GetNumlock
If GetNumlock = False Then
SetNumlock (True)
Else
Exit Sub
End If

Call GetScrollLock
If GetScrollLock = True Then
setscrolllock (False)
Else
Exit Sub
End If






Iouri Boutchkine
iouri@hotsheet.com

vb.elmar
October 19th, 2001, 04:06 PM
With your code, i have the same problem.

When i start Notepad writing sth. in notepad
and pressing the scroll button, your program
no more detects the correct scroll lock state ...