.NET

Reply
Distinguished Contributor
BMcAnney
Posts: 406
Registered: ‎12-09-2003
Message 1 of 5 (451 Views)
Accepted Solution

Can't capture key events in windows form

451 Views, 4 Replies
03-15-2012 01:36 PM

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:

 

 

        <CommandMethod("myCommand")> _
        Public Sub myCommand()
            Dim myForm As Form1 = New Form1
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myForm)
        End Sub

 

Then in the form code, I have this, for testing purposes:

 

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        MsgBox("keypress")
    End Sub

 

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 event handlers. Does anyone have a clue what I'm doing wrong here?

 

Thanks!

Hi:

 

You need to set the KeyPreview form's property to true, and then capture the keydown event with something like:

 

Private Sub MyForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

 

If e.KeyCode = Windows.Forms.Keys.Escape Then
 Me.Close()
End If


End Sub

 

Gaston Nunez

Distinguished Mentor
gasty1001
Posts: 531
Registered: ‎04-11-2010
Message 2 of 5 (441 Views)

Re: Can't capture key events in windows form

03-15-2012 09:16 PM in reply to: BMcAnney

Hi:

 

You need to set the KeyPreview form's property to true, and then capture the keydown event with something like:

 

Private Sub MyForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

 

If e.KeyCode = Windows.Forms.Keys.Escape Then
 Me.Close()
End If


End Sub

 

Gaston Nunez

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 5 (436 Views)

Re: Can't capture key events in windows form

03-15-2012 11:05 PM in reply to: BMcAnney

Here is code snip to capture KeyPress on numeric only textbox

which is allow you to jump to the next textbox on form after click Enter:

	string DSep = ',';

	private void txtBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
	{
		if (char.IsNumber(e.KeyChar) == false & 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;
			}
		}

	}

 Hope that helps

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Distinguished Contributor
BMcAnney
Posts: 406
Registered: ‎12-09-2003
Message 4 of 5 (426 Views)

Re: Can't capture key events in windows form

03-16-2012 05:43 AM in reply to: gasty1001

Thank you, Gaston! The KeyPreview setting for the form is all I needed to change. It was off by default.

 

Brent

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 5 of 5 (415 Views)

Re: Can't capture key events in windows form

03-16-2012 10:31 AM in reply to: BMcAnney

If all you want to do is close the form on the Escape key, you don't have to catch any key events.  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.

Dave O.                                                                  Sig-Logos32.png
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.