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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
danelroismanCommented:
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RuchiCommented:
GreatOne: Where are you? Please say something so we could be of any help to you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.