{"id":66583,"date":"2006-08-29T15:29:00","date_gmt":"2006-08-29T15:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/29\/how-can-i-cause-a-script-to-run-inside-an-hta-any-time-a-user-presses-enter\/"},"modified":"2006-08-29T15:29:00","modified_gmt":"2006-08-29T15:29:00","slug":"how-can-i-cause-a-script-to-run-inside-an-hta-any-time-a-user-presses-enter","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-cause-a-script-to-run-inside-an-hta-any-time-a-user-presses-enter\/","title":{"rendered":"How Can I Cause a Script to Run Inside an HTA Any Time a User Presses ENTER?"},"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 cause a script to run inside an HTA any time a user presses ENTER?<BR><BR>&#8212; JJ<\/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, JJ. Before answering your question we should state, for the record, that the Scripting Guys are unanimously opposed to the recent decision by the International Astronomical Union (IAU) to remove Pluto from the list of planets. (OK, technically the Scripting Guy who writes this column didn\u2019t actually ask any of the other Scripting Guys for their opinion. But seeing as how the Scripting Guy who writes this column is always right, well, there\u2019s very little chance they\u2019d disagree with him, is there?)<\/P>\n<P>Admittedly, some of this displeasure is personal in nature: having gotten an A in elementary school science, in part because he could name all nine of the planets, the Scripting Guy who writes this column is afraid someone will retroactively re-score all his tests and give him a B+ instead. More important, however, there\u2019s little doubt that the IAU (which made this decision based on a vote in which only 5% of its members participated) are picking on Pluto simply because it\u2019s smaller than all the other planets. What kind of message is <I>that<\/I> sending to the rest of the universe? To tell you the truth, IAU, we\u2019re not impressed; why don\u2019t you tell Jupiter or Saturn that <I>they<\/I> aren\u2019t planets? Go ahead; we dare you.<\/P>\n<P>That\u2019s what we thought.<\/P>\n<P>Besides, if we feel the need to get rid of a planet, we should be giving Uranus the boot, not Pluto. Not only does Uranus have a name that leaves every English-speaking grade school boy convulsing on the floor with laughter, but Uranus can\u2019t even stand up straight. Instead, it actually floats through space lying on its side, effectively having an East and a West pole rather than North and South poles. Come on, IAU: you call <I>that<\/I> a planet? Sheesh.<\/P>\n<P>Venus? Listen, don\u2019t get us started on <I>Venus<\/I>.<\/P>\n<P>Oh, well. In theory, we suppose that some of you might be more interested in knowing how to run a script any time a user presses the ENTER key than you are in hearing the Scripting Guys ramble on and on about the planet Pluto \u2013 excuse us: about the <I>dwarf planet<\/I> Pluto. If that\u2019s the case then this little block of code should be of interest to you:<\/P><PRE class=\"codeSample\">&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub RunScript\n        If window.event.Keycode = 13 Then\n            Msgbox &#8220;You pressed the Enter key.&#8221;\n        End If\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body onkeypress=&#8221;RunScript&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<TABLE id=\"EUD\" 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>. Yes, we know: it\u2019s <I>not<\/I> a very long script, is it? But that\u2019s OK; unlike certain astronomical associations, the Scripting Guys won\u2019t discriminate against something just because it\u2019s not very big.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you can probably tell, this is one of the least-useful HTML Applications (HTAs) you\u2019ll ever see; that\u2019s because it doesn\u2019t actually do anything other than demonstrate how you can trap and respond to the ENTER key being pressed. But we figured that would be good enough: having seen how the basic idea works you should be able to transfer the concept to an HTA that does a little bit more than just pop up a message box each time you press ENTER. All you\u2019ll have to do is replace our code (a single line that displays a message box) with your code. Is that OK with everyone? Excellent.<\/P>\n<P>So how <I>do<\/I> we trap and respond to the ENTER key being pressed? To begin with, you might have noticed that the body of HTA consists entirely of a beginning and an ending &lt;body&gt; tag. Before you dismiss that, however, take a closer look at the <B>onkeypress<\/B> event added to the &lt;body&gt; tag:<\/P><PRE class=\"codeSample\">&lt;body onkeypress=&#8221;RunScript&#8221;&gt;\n<\/PRE>\n<P>What does onkeypress do? It does this: each time a user presses a key on the keyboard this event triggers a subroutine named RunScript. (And because the onkeypress event is attached to the &lt;body&gt; tag this happens regardless of which control, if any, has the focus at the time someone presses a key.)<\/P>\n<P>Ah, good point: the only key we really care about is the ENTER key, isn\u2019t it? To tell you the truth, we don\u2019t know a straightforward way to trigger a subroutine if the ENTER key (and only the ENTER key) is pressed. Therefore we took the easy way out and we call the subroutine whenever <I>any<\/I> key on the keyboard gets pressed.<\/P>\n<P>But don\u2019t panic: that doesn\u2019t mean a message box is going to pop up each time a user presses a key on the keyboard. Instead, take a look at the first line in the subroutine:<\/P><PRE class=\"codeSample\">If window.event.Keycode = 13 Then\n<\/PRE>\n<P>What we\u2019re doing here is grabbing the <B>window.event<\/B> object (an object created any time an event \u2013 a key press, a mouse click, etc. \u2013 occurs in the HTA) and then examining the value of the <B>Keycode<\/B> property. It should come as no great shock that the Keycode property represents the Unicode key code associated with the keyboard key that triggered the event. Here we\u2019re checking to see if the Keycode property is equal to 13, which just happens to be the value assigned to the ENTER key. If the Keycode equals 13 that means the user pressed ENTER; in turn, we display a message box to that effect. If the Keycode is anything <I>but<\/I> 13 we do nothing at all. Technically the subroutine gets called any time a key (any key) gets pressed on the keyboard. As far as the user experience goes, however, nothing ever happens unless the ENTER key gets pressed. To the user it looks like the subroutine is triggered only when he or she presses the ENTER key.<\/P>\n<P>Sure, it\u2019s a bit of a workaround. But as long as it performs the task we need it to perform, well, what difference does it make? <\/P>\n<P>So how did we know that a Keycode value of 13 meant that ENTER had been pressed? We didn\u2019t; that\u2019s why we looked up Internet Explorer\u2019s <A href=\"http:\/\/msdn.microsoft.com\/workshop\/author\/dhtml\/reference\/charsets\/charset1.asp\" target=\"_blank\"><B>ISO Latin-1 Character Set<\/B><\/A>. This is a good resource if you\u2019re interested in responding to other keys and keystrokes; however, we\u2019d encourage you to use it as soon as possible. After all, you never know when the IAU will decide that <I>a<\/I>, <I>G<\/I>, <I>6<\/I>, <I>w<\/I>, and <I>@<\/I> are no longer valid keys on the keyboard.<\/P>\n<P>Anyway, JJ, this should get you started with your own HTA that runs a subroutine whenever someone presses ENTER. And, yes, you\u2019re absolutely right: the planet Mercury <I>was<\/I> named after the Roman god of thieves. And the Roman god Saturn? He\u2019s best known for <I>eating his own children<\/I>.<\/P>\n<P>Just something we thought you \u2013 and the International Astronomical Union \u2013 should know<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I cause a script to run inside an HTA any time a user presses ENTER?&#8212; JJ Hey, JJ. Before answering your question we should state, for the record, that the Scripting Guys are unanimously opposed to the recent decision by the International Astronomical Union (IAU) to remove Pluto from the [&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":[2,3,4,5,30],"class_list":["post-66583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I cause a script to run inside an HTA any time a user presses ENTER?&#8212; JJ Hey, JJ. Before answering your question we should state, for the record, that the Scripting Guys are unanimously opposed to the recent decision by the International Astronomical Union (IAU) to remove Pluto from the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66583","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=66583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66583\/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=66583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}