{"id":9631,"date":"2015-08-17T06:00:00","date_gmt":"2015-08-17T13:00:00","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=9631"},"modified":"2015-09-17T09:41:35","modified_gmt":"2015-09-17T16:41:35","slug":"typing-enter-pushes-a-button","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2015\/08\/17\/typing-enter-pushes-a-button\/","title":{"rendered":"Typing Enter Pushes a Button"},"content":{"rendered":"<p>As a beginner developer of PowerShell GUIs, I&#8217;m always looking for easy ways to make my simple applications more sophisticated and usable. One of the basic usability features in most applications is typing &lt;Enter&gt; in a textbox to invoke a function instead of clicking a Start, Go, or OK button.<\/p>\n<p>In this post, I&#8217;ll show you two ways to do it. One technique is significantly simpler than the other, but works only when there is exactly one &#8220;Go&#8221; button. The more complex technique works for multiple buttons and keys other than &lt;Enter&gt;.<\/p>\n<h3>Registering a KeyDown Event Handler<\/h3>\n<p>Let&#8217;s start with the general, more complex technique. We&#8217;ll create a KeyDown event handler for the &lt;Enter&gt; key, but you can use the same method to register an event handler for any key. Our event handler will click a particular button, but you can set it up to click any button on the user&#8217;s behalf.<\/p>\n<p>Here&#8217;s the typical situation. In this little app, when you type a valid value in the textbox, the <b>Get-Service<\/b> button is enabled. You can click the Get-Service button, but it would be nice to let the user just type &lt;Enter&gt; after typing the computer name.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/GetServicePsf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9633\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/GetServicePsf-300x89.png\" alt=\"GetServicePsf\" width=\"300\" height=\"89\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/GetServicePsf-300x89.png 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/GetServicePsf.png 559w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>To enable the &#8220;press &lt;Enter&gt;&#8221; feature, use the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.control.keydown(v=vs.110).aspx\" target=\"_blank\"><b>KeyDown<\/b> event<\/a> of the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.textbox(v=vs.110).aspx\" target=\"_blank\">Textbox object<\/a> (System.Windows.Forms.Textbox):<\/p>\n<p>1. Start by creating a variable to hold the <b>KeyDown<\/b> event of the textbox. Following the best practice naming guidelines, I&#8217;ll name the variable <b>$textboxComputerName_KeyDown<\/b>.<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName_KeyDown = {}<\/pre>\n<p>2. The <b>KeyDown<\/b> event handler requires a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.keyeventhandler(v=vs.110).aspx\"><b>KeyEventHandler<\/b> object<\/a>, so we&#8217;ll cast the script block as a <b>KeyEventHandler<\/b> object.<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{}<\/pre>\n<p>3. When the <strong>KeyCode<\/strong> property value ($_.KeyCode) is &#8220;Enter&#8221;, we want to click the Get-Service button on the user&#8217;s behalf. To click the button, call the Invoke() method of the Click event of the button ($buttonGetService.Click). Referencing the Click event without calling the Invoke() method has no effect.<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{\r\n    if ($_.KeyCode -eq 'Enter')\r\n    {\r\n        $buttonGetServices_Click.Invoke()\r\n    }\r\n}<\/pre>\n<p>Or, you can use the PerformClick() method of the button. This makes your intent much clearer than just invoking an event handler function. (Many thanks to Charlie Eberly for this suggestion!)<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{\r\n    if ($_.KeyCode -eq 'Enter')\r\n    {\r\n        $buttonGetServices.PerformClick()\r\n    }\r\n}<\/pre>\n<p>4. Set the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.keyeventargs.suppresskeypress(v=vs.110).aspx\"><b>SuppressKeyPress<\/b> property<\/a> of the <strong>KeyEventArgs<\/strong> object to $True.<\/p>\n<p>When you invoke the <b>Click<\/b> event of the button on the user&#8217;s behalf, you want to prevent the user from clicking the button, too. Otherwise, the button could be clicked multiple times. Also, the Click generates a sound that&#8217;s typically associated with clicking a button, but not with pressing &lt;Enter&gt;. Setting <strong>SuppressKeyPress<\/strong> to $True takes care of this for you.<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{\r\n\r\n    if ($_.KeyCode -eq 'Enter')\r\n    {\r\n        $buttonGetServices_Click.Invoke()\r\n\r\n        #Suppress sound from unexpected use of enter on keyPress\/keyUp\r\n        $_.SuppressKeyPress = $true\r\n    }\r\n}<\/pre>\n<p>The KeyDown event handler is now complete.<\/p>\n<p>The final step is to register the <strong>KeyDown<\/strong> event handler, that is, to associate the event hander with the <strong>KeyDown<\/strong> event so that when the <strong>KeyDown<\/strong> event happens (&#8220;is raised&#8221;), the system runs the event handler code. Because <strong>KeyDown<\/strong> is not the default event of a Textbox object, <i>PowerShell Studio<\/i> doesn&#8217;t do it for you, but it&#8217;s very easy to do.<\/p>\n<p><b>To register the event handler in PowerShell Studio:<\/b><\/p>\n<p>1. In <i>PowerShell Studio<\/i>, in the Designer window, click the textbox.<\/p>\n<p>2. In the Properties pane for the textbox, on the row for the <b>KeyDown<\/b> event, type the name of the event handler.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/clip_image004.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"margin: 0px; display: inline; background-image: none;\" title=\"clip_image004\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/clip_image004_thumb.jpg\" alt=\"clip_image004\" width=\"202\" height=\"244\" border=\"0\" \/><\/a><\/p>\n<p><b>To register the event manually <\/b>(without <i>PowerShell Studio<\/i>)<b>:<\/b><\/p>\n<p>1. Write a statement that registers the event handler.<\/p>\n<p>The syntax is:<\/p>\n<pre lang=\"PowerShell\"><FormControl>.add<EventName>()<\/pre>\n<p>For example:<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName.add_KeyDown($textboxComputerName_KeyDown)<\/pre>\n<p>2. Remember to remove the event handler in your cleanup step.<\/p>\n<p>For example:<\/p>\n<pre lang=\"PowerShell\">$textboxComputerName.remove_KeyDown($textboxComputerName_KeyDown)<\/pre>\n<h3>Set the Accept Button of the Form<\/h3>\n<p>Writing a KeyDown event is conceptually a bit complex, but it&#8217;s not difficult to do. However, the other technique is so simple that you&#8217;ll use it whenever you can.<\/p>\n<p>Every form has an <b>AcceptButton<\/b> property that is invoked automatically when the user types &lt;Enter&gt;. Typically, the <strong>AcceptButton<\/strong> property is set to the OK button, if there is one, but you can set it to any button on the form (or not set it).<\/p>\n<p>NOTE: In PowerShell Studio, when you use a form template that includes an OK button, such as a Dialog Style form, the AcceptButton is set to the OK button by default.<\/p>\n<p>In the <i>PowerShell Studio<\/i> designer and in the running application, the <b>AcceptButton<\/b> is indicated by a blue border.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/clip_image006.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"margin: 0px; display: inline; background-image: none;\" title=\"clip_image006\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/clip_image006_thumb.jpg\" alt=\"clip_image006\" width=\"244\" height=\"67\" border=\"0\" \/><\/a><\/p>\n<p><b>To set the AcceptButton property of the form:<\/b><br \/>\n&#8212; In the Properties box for the form, in the row for the AcceptButton property, use the drop-down menu to select a button.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/clip_image007.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"clip_image007\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/08\/clip_image007_thumb.png\" alt=\"clip_image007\" width=\"173\" height=\"244\" border=\"0\" \/><\/a><\/p>\n<p>-or-<\/p>\n<p>&#8212; In the Load event of the form, set the <b>AcceptButton<\/b> property of the form to the desired button.<\/p>\n<pre lang=\"PowerShell\">$formGetService_Load = {\r\n    $formGetService.AcceptButton = $buttonGetService\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Now that you know both techniques, remember not to use them together. If the <strong>AcceptButton<\/strong> property of the form is set to <em>any<\/em> button, that property value takes precedence over a <strong>KeyDown<\/strong> event with an &#8216;Enter&#8217; key code. For example, if you write a <strong>KeyDown<\/strong> event for\u00a0$buttonGetServices\u00a0and set the <strong>AcceptButton<\/strong> property of the form\u00a0is $buttonOK, when you click enter in the textbox, it clicks $buttonOK.<\/p>\n<p>Allowing the user to press &lt;Enter&gt; in your app, instead of forcing them to click a button, improves the usability of your app. Spend an extra few minutes setting the <b>AcceptButton<\/b> property, or even registering a <b>KeyDown<\/b> event.<\/p>\n<p><i>June Blender is a technology evangelist at SAPIEN Technologies, Inc and a Windows PowerShell MVP. You can reach her at <a href=\"mailto:juneb@sapien.com\" target=\"_blank\">juneb@sapien.com<\/a> and follow her on Twitter at <a href=\"http:\/\/twitter.com\/juneb_get_help\" target=\"_blank\">@juneb_get_help<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a beginner developer of PowerShell GUIs, I&#8217;m always looking for easy ways to make my simple applications more sophisticated and usable. One of the basic usability features in most applications is typing &lt;Enter&gt; in a textbox to invoke a function instead of clicking a Start, Go, or OK button. In this post, I&#8217;ll show [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[932,946,703,25],"tags":[135,934,988,1016],"class_list":["post-9631","post","type-post","status-publish","format-standard","hentry","category-beginners","category-guiprogramming","category-powershell-studio","category-windows-powershell","tag-beginner","tag-juneb","tag-powershell-gui","tag-powershell-studio"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/9631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=9631"}],"version-history":[{"count":41,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/9631\/revisions"}],"predecessor-version":[{"id":9893,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/9631\/revisions\/9893"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=9631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=9631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=9631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}