Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable normal behavior of Alt key

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.

like image 480
Jum Remdesk Avatar asked Sep 15 '25 08:09

Jum Remdesk


2 Answers

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());
like image 75
Walt D Avatar answered Sep 17 '25 02:09

Walt D


You can try something like this:

public void HandleKeyDown(object sender, keyEventArgs e)
{
  //do whatever you want with or without Alt
  
  if (e.Modifiers == Keys.Alt)
    e.SuppressKeyPress = true;
}

This should allow you to use Alt for whatever you want but keep it from activating the menustrip. Note that e.SuppressKeyPress = true also sets e.Handled = true. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keyeventargs.suppresskeypress?view=windowsdesktop-5.0

like image 35
sterlingberger Avatar answered Sep 17 '25 03:09

sterlingberger