Hey, Scripting Guy! How can I tell whether the NumLock key on a computer is on or off?
— KA
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?
0 comments