[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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.
0
Hepen
Asked:
Hepen
  • 2
1 Solution
 
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
 
anyoneisCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now