Tuesday, May 3, 2011

How can I prevent the keydown event of a form in C# from firing more than once?

According to the official documentation, the KeyDown event on a Windows Forms control occurs only once, but it is easy to demonstrate that the event fires continually aslong as a key is held down:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        label1.Text = string.Format("{0}", globalCounter++);
    }

How can you consume the event so that it fires only once?

From stackoverflow
  • You can override a ProcessCmdKey method.

  • since the multiple occourrence of KeyDown is due to the keyrepeat settings of Windows, I think that you should somehow track the KeyUp event of that key also to know that the key has been released.

  • you could use a counter!

  • Use KeyUp.

    JannieT : I want an easter-egg like feature to be enabled if the user press and hold down a certain key for more than 5 seconds. So KeyUp is not an option.
    Stevo3000 : @JannieT - Record the time of the keydown, and then process this when the keyup is triggered.
    JannieT : Can you do this without introducing a global (class scope) variable?
    Stevo3000 : I can't offhand think of a way to do this without using a member variable. The functionality could be wrapped up into a class that your form could use to encapsulate the logic behind key presses.
  • I'm generally a VB guy, but this seems to work for me as demo code, using the form itself as the input source:

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private bool _keyHeld;
            public Form1()
            {
                InitializeComponent();
                this.KeyUp += new KeyEventHandler(Form1_KeyUp);
                this.KeyDown += new KeyEventHandler(Form1_KeyDown);
                this._keyHeld = false;
            }
    
            void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                this._keyHeld = false;
            }
    
            void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (!this._keyHeld)
                {
                    this._keyHeld = true;
                    if (this.BackColor == Control.DefaultBackColor)
                    {
                        this.BackColor = Color.Red;
                    }
                    else
                    {
                        this.BackColor = Control.DefaultBackColor;
                    }
                }
                else
                {
                    e.Handled = true;
                }
            }
        }   
    }
    

    I think the logic gets a little sketchy if you're holding down multiple keys at a time, but that seems to only fire the event from the last key that was pressed anyway, so I don't think it becomes an issue.

    I tested this in a TextBox in VB, and it worked fine. Wasn't sure on the inheritance conventions I should follow in c#, so I left it as a straight Form for this answer.

    Apologies for any gross code formatting errors, again, this isn't my usual language.

    JannieT : Thanks Frosty840! Sorry about having to use a global variable, though.

0 comments:

Post a Comment