2292
2292

Reputation: 65

Detect combined keys pressed

I want to detect if the user clicked few keys together (example: ctrl+c , alt +F4 etc..)

How can I do it?

I googled it but I couldnt get any right answer

I the following code:

if (Control.ModifierKeys == (Keys.C) &&Control.ModifierKeys == Keys.Control)
{
    MessageBox.Show("Test");
}

But it didn't worked, any ideas?

Upvotes: 1

Views: 157

Answers (3)

Alexandre
Alexandre

Reputation: 1

On the constructor do this:

 public Form1()
    {
        InitializeComponent();
        this.KeyDown += new KeyEventHandler(detectCopy);
        KeyPreview = true;
    }

And in the code do this:

void detectCopy(object sender, KeyEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
    {
    //work here
    }
}

In Windows Form, the Propertie KeyPreview need be true to KeyDown Event works!!!

Upvotes: 0

Sievajet
Sievajet

Reputation: 3533

Your if statement is incorrent use this:

if (e.KeyCode == Keys.C && Control.ModifierKeys == Keys.Control)
{
}

Upvotes: 1

2292
2292

Reputation: 65

I just understood what I was should to do:
I was needed to type this:

void detectCopy(object sender, KeyEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.C)
    {
        //work here
    }
}

and this on the constructor:

public Form1()
{
    InitializeComponent();
    this.KeyDown += new KeyEventHandler(detectCopy);
}

Upvotes: 1

Related Questions