Home > Net >  How to suppress ALT from an ALT mouse combination?
How to suppress ALT from an ALT mouse combination?

Time:01-18

I can consume key combinations with e.SuppressKeyPress = true but if I need ALT LMB for example, how can I consume ALT so it doesn't activate the menu? Because when LMB is pressed the context is no longer in the KeyDown event.

CodePudding user response:

Found a solution and it's quite simple:

  1. Have a flag in a mouse event callback or override to tell if ALT was used and needs to be suppressed.
  2. In KeyUp callback or override consume ALT if the flag was set. ALT alone is detected as Keys.Menu and it activates the menu only when it's up.

Here's some code snippets:

bool suppressALT = false;
...
protected override void OnKeyUp(KeyEventArgs e)
{
    // when ALT is finally up, suppress it
    // if it was used by a mouse combo
    if(e.KeyCode == Keys.Menu && suppressALT)
    {
        suppressALT = false; // clear flag so it won't suppress the menu forever
        e.SuppressKeyPress = true;
    }
    base.OnKeyUp(e);
}
...
protected override void onm ouseDown(MouseEventArgs e)
{
    ...
    // suppress ALT if mouse combo uses it
    if((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
        suppressALT = true;
    ...
    base.OnMouseDown(e);
}
protected override void onm ouseMove(MouseEventArgs e)
{
    ...
    // suppress ALT if mouse combo uses it
    if((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
        suppressALT = true;
    ...
    base.OnMouseMove(e);
}
protected override void onm ouseWheel(MouseEventArgs e)
{
    ...
    // suppress ALT if mouse combo uses it
    if((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
        suppressALT = true;
    ...
    base.OnMouseWheel(e);
}
  •  Tags:  
  • Related