Eamon
asked on
NumLock
I have a problem with some computers.
The Num Lock keeps turning off.
I don't know what is causing this ( if you could give me an idea that would be great)
to solve this problem i have put this code into a program that is always running
Sub TurnNumLockOn()
Dim o As OSVERSIONINFO
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
' NumLock handling:
NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then 'Turn numlock on
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
keys(VK_NUMLOCK) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
'Simulate Key Press
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
End Sub
basically what it does is check the operating system.
if it is 95/98 it turns num lock on
if nt/win2000/xp it simulates keystrokes to turn it on. the problem seems to be that when it checks to see if num lock is on it finds it isn't (even when it is) and runs the code which actually turns it off.
The code runs every 10 seconds and what is happening is that the num lock comes on then 10 seconds later goes off the on and so on...
this has only been a problem on nt/win2000/xp as i have not had a chance to test it on 98.
the problem seems to be with the code
NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then 'Turn numlock on
The Num Lock keeps turning off.
I don't know what is causing this ( if you could give me an idea that would be great)
to solve this problem i have put this code into a program that is always running
Sub TurnNumLockOn()
Dim o As OSVERSIONINFO
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
' NumLock handling:
NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then 'Turn numlock on
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS
keys(VK_NUMLOCK) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
'Simulate Key Press
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
End Sub
basically what it does is check the operating system.
if it is 95/98 it turns num lock on
if nt/win2000/xp it simulates keystrokes to turn it on. the problem seems to be that when it checks to see if num lock is on it finds it isn't (even when it is) and runs the code which actually turns it off.
The code runs every 10 seconds and what is happening is that the num lock comes on then 10 seconds later goes off the on and so on...
this has only been a problem on nt/win2000/xp as i have not had a chance to test it on 98.
the problem seems to be with the code
NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then 'Turn numlock on
What about turning numlock on without testing its state?
ASKER
' Declare Type for API call:
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type
' API declarations:
Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
' Constant declarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type
' API declarations:
Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
' Constant declarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS
ASKER
turning on would probably work for the 98 computer but not for the NT
It doesn't just turn it on it sends the keyspress
so firts time you press the key it turns it on next time you press it turns it off.
I am going to check what happens if i run the 98 code on an xp computer just to see what happens its a long shot though.
It doesn't just turn it on it sends the keyspress
so firts time you press the key it turns it on next time you press it turns it off.
I am going to check what happens if i run the 98 code on an xp computer just to see what happens its a long shot though.
ASKER
well that was interesting.
i ran the 98 code on an xp computer.
the code ran through ok put had no effect.
the next time it ran
NumLockState = keys(VK_NUMLOCK)
returned true meaning it thought num lock was on and so did not run again.
my problem seems to be finding out if num lock is on.
Can anyone tell me how I do this in nt/win2000/xp
i ran the 98 code on an xp computer.
the code ran through ok put had no effect.
the next time it ran
NumLockState = keys(VK_NUMLOCK)
returned true meaning it thought num lock was on and so did not run again.
my problem seems to be finding out if num lock is on.
Can anyone tell me how I do this in nt/win2000/xp
Try this
http://oldlook.experts-exchange.com/questions/20813970/how-to-use-keybd-event-to-simulate-an-extended-ascii-keycode.html
use MapVirtualKey too
http://oldlook.experts-exchange.com/questions/20813970/how-to-use-keybd-event-to-simulate-an-extended-ascii-keycode.html
use MapVirtualKey too
ie
keybd_event VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), 0, 0
keybd_event VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), 2, 0
keybd_event VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), 0, 0
keybd_event VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), 2, 0
Tried on my W2000 but it works fine ...
It seems that 'NumLockState = keys(VK_NUMLOCK)' return always true on the machines where your routine doesn't work ...
Try making a little test program that displays the result of 'NumLockState = keys(VK_NUMLOCK)' and use it on machines where you see the problem
Try making a little test program that displays the result of 'NumLockState = keys(VK_NUMLOCK)' and use it on machines where you see the problem
Are you try GetKeyState API instead of GetKeyboardState ?
The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off ¾ alternating each time the key is pressed).
Return Values
If the function succeeds, the return value specifies the status of the given virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled
The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off ¾ alternating each time the key is pressed).
Return Values
If the function succeeds, the return value specifies the status of the given virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Does the above work fine for you if you click Command1 multiple times in a row, Ark? It seems to not recognize the state of the key correctly for me (XP).
The below works for me in XP ("And 1" strips everything but the lowest bits from the return value)..
Form1:
===========
Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If (GetKeyState(vbKeyNumlock) And 1) <> 1 Then
Call keybd_event(vbKeyNumlock, 0, 0, 0)
Call keybd_event(vbKeyNumlock, 0, KEYEVENTF_KEYUP, 0)
End If
End Sub
The below works for me in XP ("And 1" strips everything but the lowest bits from the return value)..
Form1:
===========
Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If (GetKeyState(vbKeyNumlock)
Call keybd_event(vbKeyNumlock, 0, 0, 0)
Call keybd_event(vbKeyNumlock, 0, KEYEVENTF_KEYUP, 0)
End If
End Sub
It works for me (XP too)
IsBitSet is my sub from one of the old apps, actually I check lowest bit (0), so
iBitString And (2 ^ lBitNo) equivalent to iBitString And 1 '(2^0 = 1)
IsBitSet is my sub from one of the old apps, actually I check lowest bit (0), so
iBitString And (2 ^ lBitNo) equivalent to iBitString And 1 '(2^0 = 1)
[backspaces post]
Oops, didn't notice the bVal being static... thought it'd always return True. Meh @ overlooking things
Oops, didn't notice the bVal being static... thought it'd always return True. Meh @ overlooking things
ASKER
I have left out some information about the program which i did not think was important but now know is.
This program runs in the task bar. To see what was going on i put a textbox on the form and set it to the value of NumLockState.
I ran the program - i let it run for a while in the taskbar - it started turning on the numlock then off then on....
I then maximized the form so i could see what was going on.
And what do you know it started working perfectly.
I tried many other tests and the same results. It would not work properly while in the taskbar but once you brought up the form it started working properly.
Can anyone explain this.
it also seems to work fine in the taskbar when you open the project and run it but if you run the exe it does not.
I can email an example of it if anyone wants to have a look.
This program runs in the task bar. To see what was going on i put a textbox on the form and set it to the value of NumLockState.
I ran the program - i let it run for a while in the taskbar - it started turning on the numlock then off then on....
I then maximized the form so i could see what was going on.
And what do you know it started working perfectly.
I tried many other tests and the same results. It would not work properly while in the taskbar but once you brought up the form it started working properly.
Can anyone explain this.
it also seems to work fine in the taskbar when you open the project and run it but if you run the exe it does not.
I can email an example of it if anyone wants to have a look.
ASKER
Ark I used your code and it seemed to work fine thanks alot.
I will accept you answer but I just want to keep the question open a bit longer to see if there are any more comments.
Thanks to everone who helped.
I will accept you answer but I just want to keep the question open a bit longer to see if there are any more comments.
Thanks to everone who helped.
What component do I need to compile?