I am having problem with Keyboard events capturing in C#

I have a simple form windows application, on which I have put a custom control for my specific task, I have written the KeyDown Handler for Form, the problem is that the KeyDown handler is working fine with every key other than the arrows keys... The control doesn't come in to key handler? why it is so? when I remove the custom control it starts working fine?

Upvotes: 0

Views: 572

Answers (3)

ozanmuyes
ozanmuyes

Reputation: 806

You should Focus itself somewhere in the code to use ProcessCmdKey properly.

Upvotes: 0

arbiter
arbiter

Reputation: 9575

I don't see the reason why form not preview arrow keys. But anyway, if you need more low-level access to keyboard handling (in particular shortcuts handling) you can override form's ProcessCmdKey.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Left)
    {
        // your code here
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Upvotes: 0

stevehipwell
stevehipwell

Reputation: 57468

Take a look at this SO question.

Is KeyPreview on? What control has focus?

Upvotes: 1

Related Questions