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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Turning the numlock on

Status
Not open for further replies.

Roosters

Technical User
May 14, 2000
61
AU
I need to turn the numlock on via code - can't work it out using help file - can anyone help
 
Just use this command:

SendKeys "{NUMLOCK}" Bob Scriver
 
This isn't a particularly helpful comment, but what if Numlock is already on prior to the sendkeys?

I think our user is going to have to search the API.
 
beetee: If the Numlock is already on then the SendKeys doesn't seem have any effect wether positive or negative.

Do you see a problem that I don't? I am open to ideas! Bob Scriver
 
OK, I just assumed that the {Numlock} would toggle it. My mistake.
 
OK, I just assumed that the {Numlock} would toggle it. My mistake.

Strangely, after trying it (sendkeys "{NUMLOCK}") on my computer, I get the following behavior:

If numlock is already on, the LED seems to flash quickly.

If numlock is off, nothing happens.

 
Thanks Bob and beetee
The SendKeys "{NUMLOCK}" does nothing as you say Bob, I had already tried this and had the same results as you. Is there anything else I can try?
 
Here's some code I discovered at


by doing a google search on (numlock api)

Option Explicit

Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VK_NUMLOCK = &H90
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
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
Code:


Public Sub ToggleNumLock(TurnOn As Boolean)

'To turn numlock on, set turnon to true
'To turn numlock off, set turnon to false

Dim bytKeys(255) As Byte
Dim bnumLockOn As Boolean

'Get status of the 256 virtual keys
GetKeyboardState bytKeys(0)

bnumLockOn = bytKeys(VK_NUMLOCK)
Dim typOS As OSVERSIONINFO

If bnumLockOn <> TurnOn Then 'if current state <>
'requested stae

If typOS.dwPlatformId = _
VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98

bytKeys(VK_NUMLOCK) = 1
SetKeyboardState bytKeys(0)

Else '=== WinNT/2000

'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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top