How do I detect two keydowns at the same time?

How do I detect two keydowns at the same time?

Key.A and Key.S

one always fires first.
HepenAsked:
Who is Participating?
 
anyoneisConnect With a Mentor Commented:
Here's an example:

    public partial class Form1 : Form
    {
        private ArrayList myKeys = new ArrayList();

        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!myKeys.Contains(e.KeyValue))
            {
                myKeys.Add(e.KeyValue);
                DoCheck();
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            myKeys.Remove(e.KeyValue);
        }

        private void DoCheck()
        {
            foreach (Keys key in myKeys)
            {
                if (myKeys.Contains((int)Keys.A) && myKeys.Contains((int)Keys.S))
                {
                    Console.Write("The event we have all been waiting for!!!");
                }
            }
        }

    }

David
0
 
anyoneisCommented:
>one always fires first.

That's true. But you can monitor the Keyup events also, thereby detecting when both are down together.
David
0
 
HepenAuthor Commented:
really how would i do that?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.