2
votes

When pressing the Alt key, normally the focus goes to the window's menu. I need to disable it globally. Because my application works with Alt key. When the user presses some key in combination with the Alt key, my application sends another key to active application. So I just need to ignore Alt when it opens the menu.

I'm new to programming and I'm looking for VB.Net or C# code.

2
is it Windows Forms Application or WPF?Iman
@imanabid Windows Forms ApplicationJum Remdesk

2 Answers

1
votes

My first answer is to NOT use the Alt key for your program and use Ctrl instead. Blocking "normal" things from happening usually leads to pain in the future.

But if you must use the Alt key I would check out this article which uses message filters to try and intercept it at the application level.

If that doesn't do what you're looking for, you might need to look into Global hooks, this link will get you started down the path. Global hooks are generally considered evil so you should only use this if the above two suggestions don't work. You must make sure that you uninstall your hooks otherwise you might find that you need to reboot your computer often to fix weird keyboard problems.

1
votes

This works for me:

    private class AltKeyFilter : IMessageFilter
    {
        public bool PreFilterMessage(ref Message m)
        {
            return m.Msg == 0x0104 && ((int)m.LParam & 0x20000000) != 0;
        }
    }

And then you add the message filter like so:

Application.AddMessageFilter(new AltKeyFilter());