By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Triggering "KeyDown" instead of "Click" on a button by pushing "Enter."

Curtis Rutland
Expert 2.5K+
P: 3,256
OK, I have a question. I have a problem. I'm trying to catch all KeyDown events on a button, but the problem is when I press the "Enter" key, the button's "Click" event is triggered, not the "KeyDown" event. Or if it is, it isn't being caught by the handler.

Is there a way to disable the button being clicked by pushing Enter?
Jan 11 '09 #1
Share this Question
Share on Google+
3 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
Well, I discovered a workaround. I wanted to capture the "Enter" event regardless of the focus being on the button or the form.

What I did was force the focus to the form when a button was focused on.

I'd still like to know if anyone knows a better way.
Jan 11 '09 #2

Expert 100+
P: 190
Form has an "AcceptButton" property which sets the default action when Enter is pressed, and needs to be a button. (There is also a CancelButton property for when Escape is pressed.

Not sure if that helps, and not even sure if you can set it to null, but sounds as if the button in question is being set as the Form's AcceptButton.

Also, this quote from Control.KeyDown:
Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.
Jan 12 '09 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
I think that overriding IsInputKey is going to do the trick. I'll try it out. Thanks.
Jan 12 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.