Friday, 15 June 2012

.net - C#: Form receives all keypresses except the ENTER key -


I am preparing a custom calculator. Any key pressed on this calculator form should be captured at the 'form' level. At the end, I have the following code -

  Private Zero BindControlMouseClicks (Control con) {con.mouseclick + = Representative (Object Sender, MouseEventArgs e) {TriggerMouseClicked (Sender, e); }; // Force to control foreach already added (control i con.Controls) {BindControlMouseClicks (i); } // Force future controls in future. Add Control + = Representative (Object Sender, ControlEventArgs e) {BindControlMouseClicks (e.Control); }; } Private Zero TriggerMouseClicked (Object Sender, MouseEventArgs e) {} Private Zero Form 1_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; } Zero form1_primp (object sender, keypress eventsErgus E) {of string; Switch (E. catcher) {case '\ r': key = "Enter"; break; Case '': key = "SPACE"; break; Case (four) 27: key = "escape"; break; Default: key = e.KeyChar.ToString (); break; } / * Some other code * /}  

I have set up breakpoints and added debug details in the event handler function Form1_Keypress . It is astonishing that all relevant Hit the callback function except the key (digits, characters, escape, space etc) ENTER key. It also happens with the NUMPAD ENTER key.

Any ideas why only the ENTER key is being handled differently?

Edit

My form has a bunch of buttons and text boxes. When I focus on the text box, focus, callback is also called for the ENTER key! But if I put a focus on a button, no event is generated on the ENTER key press as I have set the value to KeyPreview You have to get it, whose attention is, right?

Edit 2

This happens when there are buttons on the form and at least focuses on it if any other control focuses on it If it is centered, then it works fine. I have not set the attribute of the AcceptButton . As

, you can try to use the (char) key Are there. Instead of return \ r :

  switch (e.KeyChar) {case (char) key. Return: key = "Enter"; break; Can  

be?


No comments:

Post a Comment