Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept capture TAB key in WinForms application?

I'm trying to capture the Tab key in a Windows Forms application and do a custom action when it is pressed.

I have a Form with several listViews and buttons, I've set the Form's KeyPreview property to true and when I press any other key than tab, my KeyDown event handler does get called.

But that's not true with the Tab key - I don't receive WM_KEYDOWN message even in WndProc.

Do I need to set each control inside my form - its TabStop property - to false? There must be a more elegant way than that.

Thanks.

like image 764
Axarydax Avatar asked Mar 17 '10 10:03

Axarydax


1 Answers

This is the C# code similar to the VB code given in the answer above...

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

Hope this helps...

like image 140
Ram Avatar answered Sep 26 '22 22:09

Ram