vbscript >> How to get Num Lock status

by Thomas Brooks » Tue, 03 Aug 2004 21:53:53 GMT

Using vbscript, how do I find out if the numlock key is on or off? I then
want to use sendkeys to turn it off if it is on.


vbscript >> How to get Num Lock status

by Torgeir Bakken (MVP) » Tue, 03 Aug 2004 22:29:31 GMT


You can't do that with "pure" WSH (vbscript/jscript).

But you can do this with a free 3rd party utility that you can use
from a vbscript (using e.g. the Run method):

Klocks is a Win32 commandline utility for NT4/W2K/XP that allows the
Num, Caps and Scroll lock keys to be turned on, off or toggled, or
just show the status.

This will turn on the numlock regardless of previous state:
klocks.exe +n

torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:

Similar Threads

1. How do I determine Num Lock status? - VB.Net

2. Num Lock Status Check


Check the registry setting for NumLock
Here is a script that I use to set the value on my system when I rebuild it.
Watch for Word Wraps

Mike Bouchard

Option Explicit

Dim WSHShell, NumKeyHU, NumKeyCU, NumValue, itemtype, NumOn

Set WSHShell = WScript.CreateObject("WScript.Shell")
NumKeyHU = "HKEY_USERS\.Default\Control
NumKeyCU = "HKCU\Control Panel\Keyboard\InitialKeyboardIndicators\"
itemtype = "REG_DWORD"
NumValue = "2"

NumOn = WshShell.RegRead("HKCU\Control

If NumOn <> "2" then
  WSHShell.RegWrite NumKeyHU, NumValue
  WSHShell.RegWrite NumKeyCU, NumValue
  NumOn = WshShell.RegRead(NumKeyCU)
ElseIf NumOn = "2" then
  Msgbox "Numlock is already on"
End If

If NumOn = "2" then
 Msgbox "The users numlock will turn on at bootup"
  Msgbox "The users numlock is not on"
End if

"Amit" < XXXX@XXXXX.COM > wrote in message
news:082601c34084$c43fc480$ XXXX@XXXXX.COM ...
> Is this a way to check a computer NUM LOCK status (on/off)
> via script using WMI or any other way ?

3. INS/OVER, Caps Lock, Num Lock - VB.Net

4. Programmatically Detect and Send Num Lock and Caps Lock


I am writting a software that requires me to make sure the Num Lock is
always on and Caps Lock is always off.

First, I know how to detect if Num Lock or Caps Lock is on or off (if
someone is interested, let me know and I will send you the codes). Once
we know if the stat of Num Lock/ Caps Lock is not what we desired, we
just send the Num Lock / Caps Lock key to change the stat. From most of
the news group I have searched, there are 2 ways to do this.

1) Easy way,

SendKeys.Send("{NUMLOCK}") and SendKeys.Send("{CAPSLOCK}")

Unfortunately, this does not seem to work at all even though. A lot of
these SendKeys command do work (I tried to send Tab key, Enter key and
they both work) but the Num Lock, Caps Lock and Scroll Lock does not
seem to work at all.

Did Microsoft or anyone find the problem for this and if there are any
fixes available?

2) More Complex way,

Could someone tell me what did I do wrong here, for this does not work
either... I have tested and GetKeyState works fine. When I run
SendInput command, I would get -5245152119303962624 as the result.

Declare Function GetMessageExtraInfo Lib "user32.dll" alias
"GetMessageExtraInfo" () As IntPtr

Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal
nVirtKey As Int32) As Int16

Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long,
ByRef pInputs As INPUT, ByVal cbSize As Long) As Long

    ' Constants to verify num lock, cap lock, scroll lock state
    Private Const VK_CAPSLOCK = &H14
    Private Const VK_NUMLOCK = &H90
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const INPUT_KEYBOARD = 1
    Public Const VK_SCROLL = &H91

    Public Structure INPUT
        Public type As Integer
        Public mi As MOUSEINPUT
        Public ki As KEYBDINPUT
        Public hi As HARDWAREINPUT
    End Structure

    Public Structure MOUSEINPUT
        Public dx As Integer
        Public dy As Integer
        Public mouseData As Integer
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure

    Public Structure KEYBDINPUT
        Dim wVk As Integer
        Dim wScan As Integer
        Dim dwFlags As Integer
        Dim time As Integer
        Dim dwExtraInfo As IntPtr
    End Structure

    Public Structure HARDWAREINPUT
        Public uMsg As Integer
        Public wParamL As Integer
        Public wParamH As Integer
    End Structure

    Dim myInput As Input = New Input
    Dim sizeofINPUT As Integer =

        myInput.type = INPUT_KEYBOARD
        myInput.ki.wScan = 0
        myInput.ki.time = 0
        myInput.ki.dwFlags = 0
        myInput.ki.dwExtraInfo = GetMessageExtraInfo()

    ' If Caps Lock is on, send another Caps Lock
    If GetKeyState(VK_CAPSLOCK) = 1 Then
        ' send CAPSLOCK Key Down
        myInput.ki.dwFlags = 0
        myInput.ki.wVk = VK_CAPSLOCK
        Dim myResult As Long = SendInput(1, myInput, sizeofINPUT)

        ' send CAPSLOCK Key UP
        myInput.ki.dwFlags = KEYEVENTF_KEYUP
        myInput.ki.wVk = VK_CAPSLOCK
        Dim myNextResult As Long = SendInput(1, myInput, sizeofINPUT)
    End If


Charlie Chang

5. E_FAIL status Err num -2147467259 & 3265

6. Caps lock, Numlock, Scroll lock indicators on Status Bar

7. is there a way vba can check the lock status of a record - Access Forms Programming

8. verify lock status of a record with VBA

In a multi-user environment, Access knows when a record is locked and shows 
it in with an icon on the Record Selector bar. But I don't want to show that 
bar on my form. Is there anyway to find the lock status with VBA code?