How Can Tell Whether the NumLock Key is On or Off?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell whether the NumLock key on a computer is on or off?

-- KA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KA. You know, there are two problems with being a Scripting Guy. (Three, if you count the amount we get paid for doing this.) For one thing, you always have to be on your guard whenever Dean is around. Why? Well, to name one example, as we were busy enjoying the Dinner With the Scripting Guys promotion a few months ago, Jean glanced away from her plate for just a moment. When she looked back, she discovered that Dean was eating not only his food, but her food as well. (Dean says he got confused because of all the dishes and plates that were arrayed on the table.)

For another, people always assume that you’re joking, even when you’re not. For example, suppose you asked a question like “How can I tell whether the NumLock key on a computer is on or off?” and suppose we replied “Well, the only way we know how to do that is to write a script that uses Microsoft Word.” “Oh you Scripting Guys,” you’d say, “always with the jokes.” But this time, we’re absolutely serious.

No, really; we mean it. In yet another of those Unfathomable Mysteries of Scripting, you can’t use a technology like WMI, Windows Script Host, or the Shell object to determine whether or not the NumLock key is on. And yet, you can use Microsoft Word to carry out that task:

Set objWord = CreateObject("Word.Application")
Wscript.Echo "NumLock key is on: " & objWord.NumLock

Granted, it’s a little weird; on the bright side, it’s also a very simple little script. To begin with, we create an instance of the Word.Application object. Notice, however, that – unlike most of our Microsoft Office scripts – we don’t set the Visible property to True; that’s because there’s no reason to make Word visible. That’s due, at least in part, to the fact we need Word for only one thing: to check the value of the NumLock property. To determine whether or not the NumLock key is on we only have to execute this line of code:

Wscript.Echo "Numlock key is on: " & objWord.NumLock

After that we simply call the Quit method and terminate our instance of Word.

Weird, but effective.

Incidentally, you can also use this same basic approach to determine whether or not the CapsLock key is on; the only difference is that you echo back the value of the CapsLock property:

Set objWord = CreateObject("Word.Application")
Wscript.Echo "CapsLock key is on: " & objWord.CapsLock

Now, admittedly, you probably don’t really need a script to tell whether the NumLock key (or even the CapsLock key) on your local computer is on or off; a quick glance at the keyboard will probably suffice. But that’s OK: this script can also be used to determine whether the NumLock (or CapsLock) key on a remote computer is on or off. All you have to do is pass the name of the remote computer as the second parameter to the CreateObject method. For example, this script determines the status of the NumLock key on the remote computer atl-fs-01:

Set objWord = CreateObject("Word.Application", "atl-fs-01")
Wscript.Echo "NumLock key is on: " & objWord.NumLock

Note. You say you didn’t even know that CreateObject accepted an optional second parameter? Then it’s high-time you paid a visit to Sesame Script, isn’t it?

The one thing that this script can’t do is change the status of the NumLock key: it can tell you that the NumLock key is on but it can’t turn it off for you. (That’s because the NumLock property is read-only.) About the only way we know to toggle the NumLock key on and off is to use the SendKeys method:

Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "{NUMLOCK}"

This works, although it won’t work on remote computers (SendKeys works on only the local machine). However, this might be useful in a logon script: in such a script you could check the status of the NumLock key and then, if needed, turn it on or off.

As for us, having disposed of today’s column, it’s now time for breakfast. That’s funny: we had a muffin sitting here just a moment ago. Wait a second: where is Dean?

Comments (5)

  1. jrv says:

    Get key state without word installed:


    using System;

    using System.Runtime.InteropServices;

    public static class Keyboard{

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]

    public static extern short GetKeyState(int keyCode);

    public static bool Numlock{


    return (((ushort)GetKeyState(0x90)) & 0xffff) != 0;



    public static bool CapsLock{


    return (((ushort)GetKeyState(0x14)) & 0xffff) != 0;



    public static bool ScrollLock{


    return (((ushort)GetKeyState(0x91)) & 0xffff) != 0;





    Add-Type $code




  2. says:

    From: Archives – "How Can Tell Whether the NumLock Key is On or Off?"

    Is it possible to rescript this to be used in WordPad instead of Word and to turn on NumLock if it off?

    I am in a manufacturing environment with limited application industrial pc's

    Thanks in Advance for your help.


  3. linhartr22 says:

    Not a solution if you don't happen to have a Microsoft Word license.

  4. PAReinie says:

    (I have MS Office on this Win 7 Pro computer. It’s a Dell E5450 with no NumLock, FnLock or ScrollLock indicators, only a CapsLock and Mute indicator.)

    I should know better than to ask with only a beginner understanding of PowerShell, but is there some setup I have to perform such as loading libraries before using this as a .ps1 script? I pasted the three lines of code into the ISE and when I ran it I got “Ampersand not allowed. The & operator is reserved for future use; use “&” to pass ampersand as a string.”

    So I removed the ampersand to the end of the line and then got “Set-Variable : A positional parameter cannot be found that accepts argument ‘CreateObject’.” There is also an error “The term ‘Wscript.Echo’ is not recognized as the name of a cmdlet, function, script file, or operable program.”

    Secondly, when saved as .ps1 file and right-click and pick Run with PowerShell a window popped up with errors and then went away. How is the working program supposed to display the results? I was expecting a pop-up with OK to dismiss it.

Skip to main content