<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can't capture key events in windows form in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373521#M56797</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to set the &lt;STRONG&gt;KeyPreview&lt;/STRONG&gt; form's property to true, and then capture the keydown event with something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;Private Sub MyForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;If e.KeyCode = Windows.Forms.Keys.Escape Then&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;&amp;nbsp;Me.Close()&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt; End If&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;End Sub&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Gaston Nunez&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Mar 2012 04:16:47 GMT</pubDate>
    <dc:creator>hgasty1001</dc:creator>
    <dc:date>2012-03-16T04:16:47Z</dc:date>
    <item>
      <title>Can't capture key events in windows form</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373073#M56796</link>
      <description>&lt;P&gt;I have created a basic add-on that shows a modal windows form. The only issue I am running into is that I am completely unable to capture any key events in the form. I just want to make the escape key close my form, but I'm having no luck. Here is my basic setup:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        &amp;lt;CommandMethod("myCommand")&amp;gt; _
        Public Sub myCommand()
            Dim myForm As Form1 = New Form1
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myForm)
        End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then in the form code, I have this, for testing purposes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        MsgBox("keypress")
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've also tried KeyUp and KeyDown. Key presses are working inside the form (I can use the keyboard to select an item in a combobox, for example), but I never hit the&amp;nbsp;event handlers. Does anyone have a clue what I'm doing wrong here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2012 20:36:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373073#M56796</guid>
      <dc:creator>BMcAnney</dc:creator>
      <dc:date>2012-03-15T20:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Can't capture key events in windows form</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373521#M56797</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need to set the &lt;STRONG&gt;KeyPreview&lt;/STRONG&gt; form's property to true, and then capture the keydown event with something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;Private Sub MyForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;If e.KeyCode = Windows.Forms.Keys.Escape Then&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;&amp;nbsp;Me.Close()&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt; End If&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;End Sub&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Gaston Nunez&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2012 04:16:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373521#M56797</guid>
      <dc:creator>hgasty1001</dc:creator>
      <dc:date>2012-03-16T04:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: Can't capture key events in windows form</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373613#M56798</link>
      <description>&lt;P&gt;Here is code snip to capture KeyPress on numeric only textbox&lt;/P&gt;&lt;P&gt;which is allow you to jump to the next textbox on form after click Enter:&lt;/P&gt;&lt;PRE&gt;	string DSep = ',';

	private void txtBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
	{
		if (char.IsNumber(e.KeyChar) == false &amp;amp; Microsoft.VisualBasic.AscW(e.KeyChar) != 46) {
			if (e.KeyChar == Convert.ToChar(DSep)) {
				e.Handled = true;
			} else if (Microsoft.VisualBasic.AscW(e.KeyChar) == 13) {
				this.txtBox2.Select();
				e.Handled = false;
			} else {
				e.Handled = true;
			}
		}

	}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Hope that helps&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800000" face="arial,helvetica,sans-serif"&gt;~'J'~&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2012 06:06:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373613#M56798</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-03-16T06:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Can't capture key events in windows form</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373977#M56799</link>
      <description>&lt;P&gt;Thank you, Gaston! The KeyPreview setting for the form is all I needed to change. It was off by default.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Brent&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2012 12:43:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3373977#M56799</guid>
      <dc:creator>BMcAnney</dc:creator>
      <dc:date>2012-03-16T12:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: Can't capture key events in windows form</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3374559#M56800</link>
      <description>&lt;P&gt;If all you want to do is close the form on the Escape key, you don't have to&amp;nbsp;catch any key events.&amp;nbsp; All you have to do is put a button on your form (it doesn't even need to be visible), and assign that button to the forms CancelButton property.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Mar 2012 17:31:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-capture-key-events-in-windows-form/m-p/3374559#M56800</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2012-03-16T17:31:10Z</dc:date>
    </item>
  </channel>
</rss>

