locked
KeyPreview = true, how do you then detect enter/return? RRS feed

  • Question

  • Hello all,

    So basically, I am just creating a standard calculator with 0-9, decimal point, addition and subtraction.

     

    I want the user to be able to use the keyboard, or use the buttons to enter their values. Here is the strange problem. I had to turn KeyPreview to true to start capturing the NumberPad/Keyboard, however doing that for the form seems to disable the capturing of the enter/return key. Anyone know how to do that? I have tried so many ways now, here is the code I have (which is probably all messed up now from attempting it so many different ways):

     

    http://pastebin.com/f46f1355b

     

    One other problem is the KeyCode for the "=" key. Is there not one? Does it default to enter/return?

     

     

    Thanks,

     

    Shawn

    Sunday, August 17, 2008 7:24 PM

Answers

  • Hi Smaktard,

     

    As far as I can understand, the problem you are facing is “Enter Key Down event can’t be captured when the property of KeyPreview was set to true”.

     

    It is probably the focus is on some button when you press the “Enter” key. Certain keys, such as TAB, Return, Escape and arrow keys are handled by controls automatically. The button will not raise a KeyDown event when you press the “Enter” key, so even you set the property of KeyPreview to true, the KeyDown event will not be propagated to the form.

     

    In order to make the KeyPreview property to work, we can take the following steps.

    1.       Add PreviewKeyDown event for each of the buttons.

    2.       In the PreviewKeyDown event handler, set the PreviewKeyDownEventArgs.IsInputKey to true.

    Of course there are some other ways to accomplish that.

     

    Code Snippet

             void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

             {

                if (e.KeyData == Keys.Enter)

                       e.IsInputKey = true;

                }

     

     

    I am looking for your test result. If you still have problems, please don’t hesitate to tell me.

     

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Tuesday, August 19, 2008 5:26 AM
  • to get the return/enter key, compare the e.KeyCode to Keys.Return/Keys.Enter in your Key_Up event handler just like you did in your sample, I wonder why you say it doesn't work - it works fine for me...
    to get the "=" key, compare e.KeyCode to Keys.Oemplus.

    hth,
    Lior.
    Sunday, August 17, 2008 8:58 PM

All replies

  • to get the return/enter key, compare the e.KeyCode to Keys.Return/Keys.Enter in your Key_Up event handler just like you did in your sample, I wonder why you say it doesn't work - it works fine for me...
    to get the "=" key, compare e.KeyCode to Keys.Oemplus.

    hth,
    Lior.
    Sunday, August 17, 2008 8:58 PM
  • hi,Shawn,

     

    For Enter is a CmdKey,u cann't let some controls such as [Button] get focused,

    so what u need to do is to avoid this kind of controls get focused.

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Text;
    7. using System.Windows.Forms;
    8.  
    9. namespace GaryCalculator
    10. {
    11.     public partial class Form1 : Form
    12.     {
    13.         List<float> numbers = new List<float>();
    14.        
    15.         public Form1()
    16.         {
    17.             InitializeComponent();
    18.         }
    19.  
    20.         private void Total()
    21.         {
    22.             float answer = 0;
    23.  
    24.             foreach (float number in numbers)
    25.             {
    26.                 answer += number;
    27.             }
    28.             lblOutput.Text = answer.ToString();
    29.             numbers.Clear();
    30.         }
    31.  
    32.         private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    33.         {
    34.             MessageBox.Show(e.KeyChar.ToString());
    35.         }
    36.  
    37.         private void Form1_KeyDown(object sender, KeyEventArgs e)
    38.         {
    39.             if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
    40.             {
    41.                 MessageBox.Show("Enter key");
    42.             }
    43.             if (e.KeyCode == Keys.C)
    44.             {
    45.                 lblOutput.Text = "";
    46.                 numbers.Clear();
    47.             }
    48.             else if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
    49.                 lblOutput.Text += "0";
    50.             else if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)
    51.                 lblOutput.Text += "1";
    52.             else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2)
    53.                 lblOutput.Text += "2";
    54.             else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3)
    55.                 lblOutput.Text += "3";
    56.             else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4)
    57.                 lblOutput.Text += "4";
    58.             else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5)
    59.                 lblOutput.Text += "5";
    60.             else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6)
    61.                 lblOutput.Text += "6";
    62.             else if (e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7)
    63.                 lblOutput.Text += "7";
    64.             else if (e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8)
    65.                 lblOutput.Text += "8";
    66.             else if (e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9)
    67.                 lblOutput.Text += "9";
    68.             else if (e.KeyCode == Keys.Decimal)
    69.             {
    70.                 if (!lblOutput.Text.Contains("."))
    71.                     lblOutput.Text += ".";
    72.             }
    73.             else if (e.KeyCode == Keys.Add)
    74.             {
    75.                 MessageBox.Show("Add");
    76.             }
    77.             else if (e.KeyCode == Keys.Subtract)
    78.             {
    79.                 MessageBox.Show("Subtract");
    80.             }
    81.         }
    82.  
    83.         private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    84.         {
    85.             MessageBox.Show(e.KeyCode.ToString());
    86.             lblOutput.Focus();  
    87.         }

                       private void Form1_Activated(object sender, System.EventArgs e)
                       {
                              lblOutput.Focus();  
                       }

    1.     }
    2. }
    Sunday, August 17, 2008 10:36 PM
  • Hi Smaktard,

     

    As far as I can understand, the problem you are facing is “Enter Key Down event can’t be captured when the property of KeyPreview was set to true”.

     

    It is probably the focus is on some button when you press the “Enter” key. Certain keys, such as TAB, Return, Escape and arrow keys are handled by controls automatically. The button will not raise a KeyDown event when you press the “Enter” key, so even you set the property of KeyPreview to true, the KeyDown event will not be propagated to the form.

     

    In order to make the KeyPreview property to work, we can take the following steps.

    1.       Add PreviewKeyDown event for each of the buttons.

    2.       In the PreviewKeyDown event handler, set the PreviewKeyDownEventArgs.IsInputKey to true.

    Of course there are some other ways to accomplish that.

     

    Code Snippet

             void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

             {

                if (e.KeyData == Keys.Enter)

                       e.IsInputKey = true;

                }

     

     

    I am looking for your test result. If you still have problems, please don’t hesitate to tell me.

     

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Tuesday, August 19, 2008 5:26 AM