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

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

  • Comments 3
  • Likes
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
objWord.Quit

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
objWord.Quit

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
objWord.Quit

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?

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • 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.

    Rick

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

  • Get key state without word installed:

    $code=@'

    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{

    get{

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

    }

       }

    public static bool CapsLock{

    get{

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

    }

        }

    public static bool ScrollLock{

    get{

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

    }

       }

    }

    '@

    Add-Type $code

    [Keyboard]::Numlock

    [Keyboard]::CapsLock

    [Keyboard]::ScrollLock