GetKeyState Help

I am writing a simple program that will tell me if the capslock,numlock, and scroll lock is on or off. I used the GetKeyState function to determine the status of each. However, I noticed that it always says NumLock and ScrollLock are on. I am wondering if this has anything to do with the fact that on my machine i have to hold down an Fn key and then the F11 or F12 to activate/deactivate scroll or numlock.

Does anyone know how i can determine whether or not the scroll/num lock are on or off?

Here is the code i am using:
Const VK_NUMLOCK = &H90
Const VK_OEM_SCROLL = &H91

Private Sub Form_Load()
IIf (GetKeyState(VK_NUMLOCK)=1, "ON", "OFF")
End Sub

Thanks
GreatOneAsked:
Who is Participating?
 
RuchiConnect With a Mentor Commented:
From the web:

Place the three text boxes and a timer on the form.

Option Explicit

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


Public Function CapsLockOn() As Boolean
    Dim iKeyState As Integer
    iKeyState = GetKeyState(vbKeyCapital)
    CapsLockOn = (iKeyState = 1 Or iKeyState = -127)
End Function

Public Function NumLockOn() As Boolean
    Dim iKeyState As Integer
    iKeyState = GetKeyState(vbKeyNumlock)
    NumLockOn = (iKeyState = 1 Or iKeyState = -127)
End Function

Public Function ScrlLockOn() As Boolean
    Dim iKeyState As Integer
    iKeyState = GetKeyState(vbKeyScrollLock)
    ScrlLockOn = (iKeyState = 1 Or iKeyState = -127)
End Function

Private Sub Form_Load()
   Text1.Enabled = False
   Text2.Enabled = False
   Text3.Enabled = False
   
End Sub

Private Sub tmrKeyStates_Timer()
   If NumLockOn() Then
      Text1.Text = ("NUM On")
   Else
      Text1.Text = ("NUM Off")
   End If
   
   If CapsLockOn() Then
      Text2.Text = ("CAPS On")
   Else
      Text2.Text = ("CAPS Off")
   End If
   
   If ScrlLockOn() Then
      Text3.Text = ("SCR On")
   Else
      Text3.Text = ("SCR Off")
   End If
End Sub
0
 
amebaCommented:
   CapsLock = GetKeyState(VK_CAPITAL) And 1 = 1

but this doesn't work perfect - see:
http://www.experts-exchange.com/Q.10160074
0
 
GreatOneAuthor Commented:
I am not having trouble detecting the caps lock key. I am only having trouble detecting the numlock and scroll lock. I tried using the code above, replacing VK_CAPITAL with VK_NUMLOCK but now it always says numlock is off.

Any other suggestions?
Thanks
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
danelroismanCommented:
0
 
amebaCommented:
'Re-tested
Option Explicit
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
Const VK_NUMLOCK = &H90

Private Sub Form_Click()
    Caption = IIf(GetKeyState(VK_NUMLOCK) And 1 = 1, "ON", "OFF")
End Sub
0
 
GreatOneAuthor Commented:
I have already tried that code and it doesn't work. It always says the numlock is off.
0
 
amebaCommented:
Eh, what can I say... Try again
or try my code in mentioned PAQ.
0
 
LeXienCommented:
hehe, have u got one of those doggy AST or Packard Bell Keyboards? :)

dunno, sorry.
0
 
GreatOneAuthor Commented:
ameba,

I have tried the code above and the code listed in the PAQ. Unfortunately neither of them worked on my laptop. they did work when i tried on my desktop. Any other suggestions?

Lexien,
No I don't have an AST or Packard Bell keyboard.
0
 
KhollADCommented:
Try the code below :

Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long

Private Const VK_CAPITAL = &H14
Private Const VK_NUMLOCK = &H90

Private Sub Command1_Click()

ReDim Kstate(255) As Byte
GetKeyboardState Kstate(0)

MsgBox "Numlock : " & IIf(Kstate(VK_NUMLOCK) = 1, "ON", "OFF") & vbCrLf & "CapsLock :" & IIf(Kstate(VK_CAPITAL) = 1, "ON", "OFF")

End Sub

0
 
RuchiCommented:
GreatOne: Where are you? Please say something so we could be of any help to you.
0
All Courses

From novice to tech pro — start learning today.