Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to handle an event in my c# project such that whenever the key is pressed the event should occur.For that I tried with a following sample code but its not working and neither shows any error.

C#
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Space)
           {
               MessageBox.Show("key pressed");
           }
       }

Any solution would really be a great help.
Thank you.
Posted

To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true. Read about this here[^].
 
Share this answer
 
v2
Do the following:

Set the form's KeyPreview to true so that the form can capture the Spacebar.
C#
this.KeyPreview = true;

Then use the KeyDown event of the form:
this.KeyDown += new KeyEventHandler(Form1_KeyDown);

void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                MessageBox.Show("key pressed");
            }
        }

Should work now..

And importantly set
this.KeyPreview = true;

on your form's constructor, or simply use the form designer and set the KeyPreview parameter to true.

And finally have look Here[^]
 
Share this answer
 
v2
If you wants to handle events on form then try this

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        KeyPreview = true;
        KeyDown += new KeyEventHandler(Form1_KeyDown);
    }

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        System.Diagnostics.Debug.Write(e.KeyCode);
    }
}


And for specific control you must handle event on that control.

For example :



Handle the KeyDown event to determine the type of character entered into the control.

C#
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900