{"id":55933,"date":"2008-03-25T00:24:00","date_gmt":"2008-03-25T00:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/03\/25\/hey-scripting-guy-how-can-i-add-a-hotkey-to-an-hta-button\/"},"modified":"2008-03-25T00:24:00","modified_gmt":"2008-03-25T00:24:00","slug":"hey-scripting-guy-how-can-i-add-a-hotkey-to-an-hta-button","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-add-a-hotkey-to-an-hta-button\/","title":{"rendered":"Hey, Scripting Guy! How Can I Add a Hotkey to an HTA Button?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! How can I add a hotkey to a button in my HTA?<\/p>\n<p>&#8212; TM<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, TM. As usual, the Monday column is being written on Friday; however, the Scripting Guy who writes this column is having far more trouble getting things done than he usually does. That\u2019s because today is day 2 of the NCAA basketball tournament (\u201cMarch Madness\u201d) and he\u2019s trying both to write this column and listen to \u2013 at the moment \u2013 San Diego vs. Connecticut. That\u2019s not going all that well, mainly because he finds it hard to just <i>listen<\/i> to the game; the game is being telecast over the Internet, and every time something happens the Scripting Guy who writes this column can\u2019t help but switch over the browser window and watch the replay. He then returns to writing today\u2019s column, only to remember that its time to check the South Alabama vs. Butler score (Butler 56, South Alabama 38). <\/p>\n<p>And yes, we agree: you <i>would<\/i> think Microsoft would give everyone days 1 and 2 of the tournament off, wouldn\u2019t you? Unfortunately, Microsoft seems oblivious to the NCAA basketball tournament. For example, when the Cricket World Cup was played, a gigantic TV screen was set up in the cafeteria and all the games were shown there. The same thing occurred during soccer\u2019s World Cup. But for the NCAA basketball tournament, the absolute greatest sporting event ever created? Nothing. Nada. Zip. How many people at Microsoft even <i>care<\/i> that Stephan Curry scored 40 points as Davidson rallied to beat Gonzaga 82-76? Other than the Scripting Guy who writes this column: None. Nada. Zip.<\/p>\n<p>Fortunately there <i>is<\/i> March Madness on Demand, which \u2013 despite a somewhat-choppy picture \u2013 does let the Scripting Guy who writes this column keep tabs on everything that\u2019s going on. Not that TV over the Internet is all that great, mind you; for example, if you don\u2019t go in periodically and either minimize or maximize your browser window you\u2019ll be kicked out of the broadcast \u201cviewing room.\u201d (Why? Because if there\u2019s no such window activity it\u2019s assumed that you aren\u2019t watching any more. Why? Because apparently the network believes that anyone who watches a basketball game over the Internet spends most of their time minimizing and maximizing their browser window rather than actually watching the game.) Nevertheless, it works; how else would we know that Western Kentucky sank a three-pointer at the buzzer to beat Drake in overtime?<\/p>\n<p>At the moment, however, there\u2019s a break in the action: Texas, Georgetown, and Butler all have comfortable leads, and San Diego and Connecticut are at half time. (With San Diego leading by 5. What a \u2026 shame \u2026 if Connecticut loses, eh?). That gives us just enough time to show you how to add a hotkey to an HTA button:<\/p>\n<pre class=\"codeSample\">&lt;Script Language=\"VBScript\"&gt;\n    Sub ButtonA\n        Msgbox \"Button A was clicked.\"\n    End Sub\n\n    Sub ButtonB\n        Msgbox \"Button B was clicked.\"\n    End Sub  \n\n    Sub ButtonC\n        Msgbox \"Button C was clicked.\"\n    End Sub        \n      \n&lt;\/Script&gt;\n\n&lt;body&gt;\n    &lt;button onClick=\"ButtonA\" accessKey=\"a\"&gt; Button &lt;U&gt;A&lt;\/U&gt; &lt;\/button&gt;\n    &lt;button onClick=\"ButtonB\" accessKey=\"b\"&gt; Button &lt;U&gt;B&lt;\/U&gt; &lt;\/button&gt;\n    &lt;button onClick=\"ButtonC\" accessKey=\"c\"&gt; Button &lt;U&gt;C&lt;\/U&gt; &lt;\/button&gt;\n&lt;\/body&gt;\n<\/pre>\n<p>The secret to adding a hotkey to an HTA button is this: use the <b>&lt;button&gt;<\/b> tag rather than the <b>&lt;input&gt;<\/b> tag. Why? We\u2019ll explain why in just a moment. Before we do, we should note that you might think that the best way to add a hotkey to a button is to use an HTML tag similar to this:<\/p>\n<pre class=\"codeSample\">&lt;input type=\"button\" value=\"Button A\" onClick=\"ButtonA\" accessKey=\"a\"&gt;\n<\/pre>\n<p>That actually works just fine; add that tag to your HTA, and pressing Alt+a on the keyboard is exactly the same as clicking Button A itself. The only problem with using the &lt;input&gt; tag is that you can\u2019t do anything to indicate which key is the hotkey for a given button. Instead, your HTA will look like this, and the user will have no idea that pressing Alt+a is the same thing as clicking Button A:<\/p>\n<p><img decoding=\"async\" height=\"217\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hotkey1.jpg\" width=\"290\" border=\"0\" \/> <\/p>\n<p>Needless to say, there isn\u2019t much point in adding a hotkey if no one knows that this hotkey is available to them.<\/p>\n<p>By contrast, using the &lt;button&gt; tag enables us to create a more typical user interface, one in which the hotkey is clearly labeled:<\/p>\n<p><img decoding=\"async\" height=\"217\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hotkey2.jpg\" width=\"290\" border=\"0\" \/> <\/p>\n<p>So how do we actually implement the &lt;button&gt; tag? Well, let\u2019s take a look at the code for creating Button A:<\/p>\n<pre class=\"codeSample\">&lt;button onClick=\"ButtonA\" accessKey=\"a\"&gt; Button &lt;U&gt;A&lt;\/U&gt; &lt;\/button&gt;\n<\/pre>\n<p>As you can see, we simply add the &lt;button&gt; tag along with two parameters:<\/p>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>onClick=&#8221;ButtonA&#8221;<\/b>. This is simply the name of the subroutine we want to run each time the button is clicked <i>or<\/i> each time we press the appropriate hotkey.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>accessKey=&#8221;a&#8221;<\/b>. This is the hotkey associated with the button. Note that the Alt key is assumed; we only have to specify the \u201cother\u201d key, in this case the other key being <i>a<\/i>. By the way, this is the a <i>key<\/i> and not the actual letter a; there\u2019s no difference between an uppercase A (e.g., the key being pressed while Caps Lock is on) or a lowercase a (e.g., the key being pressed while Caps Lock is off).<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In addition to adding these two parameters notice how we format the button label (that is, the text that appears on the button):<\/p>\n<pre class=\"codeSample\">Button &lt;U&gt;A&lt;\/U&gt;\n<\/pre>\n<p>Unlike the &lt;input&gt; tag, the &lt;button&gt; tag allows you to use HTML tagging as part of the label text. All we\u2019ve done here is use the underline tag (<b>&lt;U&gt;&lt;\/U&gt;<\/b>) to put an underline under the <i>A<\/i> in <i>Button A<\/i>; that\u2019s the standard way to indicate a hotkey in Windows. If you march to the beat of a different drummer, however, you can use some other method to indicate the hotkey. For example, this tagging puts the letter <i>A<\/i> in boldface:<\/p>\n<pre class=\"codeSample\">Button &lt;B&gt;A&lt;\/B&gt;\n<\/pre>\n<p>Etc., etc.<\/p>\n<p>As for the subroutines that get called when a button is clicked or a hotkey pressed, well, for this simple script the subroutines are equally simple: they pop up a message box that indicates which button was clicked. For example, here\u2019s the code for the subroutine ButtonA:<\/p>\n<pre class=\"codeSample\">Sub ButtonA\n    Msgbox \"Button A was clicked.\"\nEnd Sub\n<\/pre>\n<p>Most likely you\u2019d have your HTA do something a little fancier, and maybe even a little more practical. But that\u2019s entirely up to you.<\/p>\n<p>Halftime is over, which means it\u2019s time to go back to \u2026 work \u2026. For those of you who keep track of these things (and we know that some of you <i>do<\/i> keep track of these things) the Scripting Guy who writes this column is off to a slow start in defending his championship when it comes to picking the winners of each and every game in the tournament; as of this morning, the Scripting Son, the Scripting Brother, and the Scripting Editor (!) were tied for first place, with the Scripting Guy who writes this column trailing by 2 points. But that\u2019s OK: it\u2019s still early, and the fun doesn\u2019t <i>really<\/i> begin until round 3. The Scripting Guy who writes this column isn\u2019t worried.<\/p>\n<p>Although he <i>won\u2019t<\/i> be happy if he somehow manages to finish behind the Scripting Editor, who selected her winners based on which team has the prettiest uniforms.<\/p>\n<table class=\"dataTable\" id=\"E4F\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. OK, that\u2019s not <i>really<\/i> how the Scripting Editor selected her winners; she actually knows a little something about college basketball. However, based on his performance in the tournament so far the Scripting Guy who writes this column is beginning to wonder if that\u2019s how he should have picked <i>his<\/i> winners.<\/p>\n<p><b>Ed<\/b><b>itor\u2019s Note:<\/b> San Diego won in overtime.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a hotkey to a button in my HTA? &#8212; TM Hey, TM. As usual, the Monday column is being written on Friday; however, the Scripting Guy who writes this column is having far more trouble getting things done than he usually does. That\u2019s because today is day 2 [&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":[3,4,5,30],"class_list":["post-55933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add a hotkey to a button in my HTA? &#8212; TM Hey, TM. As usual, the Monday column is being written on Friday; however, the Scripting Guy who writes this column is having far more trouble getting things done than he usually does. That\u2019s because today is day 2 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55933","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=55933"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55933\/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=55933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}