{"id":66713,"date":"2006-08-10T16:26:00","date_gmt":"2006-08-10T16:26:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/10\/how-can-tell-whether-the-numlock-key-is-on-or-off\/"},"modified":"2006-08-10T16:26:00","modified_gmt":"2006-08-10T16:26:00","slug":"how-can-tell-whether-the-numlock-key-is-on-or-off","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-tell-whether-the-numlock-key-is-on-or-off\/","title":{"rendered":"How Can Tell Whether the NumLock Key is On or Off?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I tell whether the NumLock key on a computer is on or off?<BR><BR>&#8212; KA<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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.)<\/P>\n<P>For another, people always assume that you\u2019re joking, even when you\u2019re not. For example, suppose you asked a question like \u201cHow can I tell whether the NumLock key on a computer is on or off?\u201d and suppose we replied \u201cWell, the only way we know how to do that is to write a script that uses Microsoft Word.\u201d \u201cOh you Scripting Guys,\u201d you\u2019d say, \u201calways with the jokes.\u201d But this time, we\u2019re absolutely serious.<\/P>\n<P>No, really; we mean it. In yet another of those Unfathomable Mysteries of Scripting, you can\u2019t 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 <I>can<\/I> use Microsoft Word to carry out that task:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nWscript.Echo &#8220;NumLock key is on: &#8221; &amp; objWord.NumLock\nobjWord.Quit\n<\/PRE>\n<P>Granted, it\u2019s a little weird; on the bright side, it\u2019s also a very simple little script. To begin with, we create an instance of the <B>Word.Application<\/B> object. Notice, however, that \u2013 unlike most of our <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/office.mspx\"><B>Microsoft Office scripts<\/B><\/A> \u2013 we don\u2019t set the Visible property to True; that\u2019s because there\u2019s no reason to make Word visible. That\u2019s due, at least in part, to the fact we need Word for only one thing: to check the value of the <B>NumLock<\/B> property. To determine whether or not the NumLock key is on we only have to execute this line of code:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Numlock key is on: &#8221; &amp; objWord.NumLock\n<\/PRE>\n<P>After that we simply call the <B>Quit<\/B> method and terminate our instance of Word.<\/P>\n<P>Weird, but effective.<\/P>\n<P>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 <B>CapsLock<\/B> property:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nWscript.Echo &#8220;CapsLock key is on: &#8221; &amp; objWord.CapsLock\nobjWord.Quit\n<\/PRE>\n<P>Now, admittedly, you probably don\u2019t 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\u2019s 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 <B>CreateObject<\/B> method. For example, this script determines the status of the NumLock key on the remote computer atl-fs-01:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;, &#8220;atl-fs-01&#8221;)\nWscript.Echo &#8220;NumLock key is on: &#8221; &amp; objWord.NumLock\nobjWord.Quit\n<\/PRE>\n<TABLE id=\"EDE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. You say you didn\u2019t even <I>know<\/I> that CreateObject accepted an optional second parameter? Then it\u2019s high-time you paid a visit to <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/begin\/ss0406.mspx\"><B>Sesame Script<\/B><\/A>, isn\u2019t it?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The one thing that this script <I>can\u2019t<\/I> do is change the status of the NumLock key: it can tell you that the NumLock key is on but it can\u2019t turn it off for you. (That\u2019s 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 <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_wsh_hilv.mspx\"><B>SendKeys<\/B><\/A> method:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;WScript.Shell&#8221;)\nobjShell.SendKeys &#8220;{NUMLOCK}&#8221;\n<\/PRE>\n<P>This works, although it <I>won\u2019t<\/I> 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.<\/P>\n<P>As for us, having disposed of today\u2019s column, it\u2019s now time for breakfast. That\u2019s funny: we had a muffin sitting here just a moment ago. Wait a second: where <I>is<\/I> Dean?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell whether the NumLock key on a computer is on or off?&#8212; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,84,49,3,5],"class_list":["post-66713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell whether the NumLock key on a computer is on or off?&#8212; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}