How to set hotkeys for a Windows Forms form

后端 未结 8 1692
孤街浪徒
孤街浪徒 2020-12-03 05:22

I would like to set hotkeys in my Windows Forms form. For example, Ctrl + N for a new form and Ctrl + S for save. How would I do

8条回答
  •  离开以前
    2020-12-03 05:43

    You can also override ProcessCmdKey in your Form derived type like this:

    protected override bool ProcessCmdKey(ref Message message, Keys keys)
    {
        switch (keys)
        {
            case Keys.B | Keys.Control | Keys.Alt | Keys.Shift:
                // ... Process Shift+Ctrl+Alt+B ...
    
                return true; // signal that we've processed this key
        }
    
        // run base implementation
        return base.ProcessCmdKey(ref message, keys);
    }
    

    I believe it's more suitable for hotkeys. No KeyPreview needed.

提交回复
热议问题