.net - C#: Form receives all keypresses except the ENTER key -
i designing custom calculator. key pressed on calculator form should captured @ 'form' level. end, i've following code-
private void bindcontrolmouseclicks(control con) { con.mouseclick += delegate(object sender, mouseeventargs e) { triggermouseclicked(sender, e); }; // bind controls added foreach (control in con.controls) { bindcontrolmouseclicks(i); } // bind controls added in future con.controladded += delegate(object sender, controleventargs e) { bindcontrolmouseclicks(e.control); }; } private void triggermouseclicked(object sender, mouseeventargs e) { } private void form1_load(object sender, eventargs e) { this.keypreview = true; this.keypress +=new keypresseventhandler(form1_keypress); bindcontrolmouseclicks(this); this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog; this.maximizebox = false; } void form1_keypress(object sender, keypresseventargs e) { string key; switch (e.keychar) { case '\r': key = "enter"; break; case ' ': key = "space"; break; case (char)27: key = "escape"; break; default: key = e.keychar.tostring(); break; } /* other code */ }
i've set breakpoints , added debug statements in event handler function form1_keypress
. surprisingly relevant keys ( numerals, alphabets, escape, space etc ) hit callback function except enter key. happens numpad enter key too.
any ideas why enter key being handled differently?
edit
my form has bunch of buttons , textbox. when place focus on textbox, voila, callback called enter key too! if put focus on of buttons, no event generated on enter key press. i've set keypreview
true, form must getting event irrespective of focus is, right?
edit 2
this happens when there buttons on form , @ least on of them has focus on it. if other control has focus on it, works fine. have not set acceptbutton
attribute of form.
as msdn says(.net textbox - handling enter key), can try use (char)keys.return
instead of \r
:
switch (e.keychar) { case (char)keys.return: key = "enter"; break;
also may there some button acceptsreturn property set true on form?
Comments
Post a Comment