Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accelerator (mnemonic key) are executed without pressing ALT key

I'm experiencing some troubles using mnemonic keys in Windows Forms:

Simply having a form with a button, which uses ALT+s as the accelerator:

this.searchButton = new System.Windows.Forms.Button();
this.searchButton.Text = "&search";

The button action is executed by simply pressing "s" (without pressing the ALT key). I have checked some other applications, and the accelerator actions are only executed when the ALT key is pressed.

  • Is this a .NET problem?
  • How could address this issue?

Thanks in advance.

like image 537
Daniel Peñalba Avatar asked Dec 21 '11 17:12

Daniel Peñalba


1 Answers

You can alter this behavior by pasting this snippet into your form:

    protected override bool ProcessDialogChar(char charCode) {
        if ((Control.ModifierKeys & Keys.Alt) == Keys.None) return false;
        return base.ProcessDialogChar(charCode);
    }

Not 100% sure this won't have other side-effects, keyboard handling in Winforms is convoluted to put it mildly.

like image 195
Hans Passant Avatar answered Oct 01 '22 02:10

Hans Passant