winform 快捷键的两种实现方法

  • 方法1:只当焦点在当前控件上有效
        private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Dispose();
            if (e.KeyCode == Keys.Space)
                pictureBox_relocation();
        }
  • 方法2:当前窗体处于激活状态有效
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            KeyEventArgs e = new KeyEventArgs(keyData);
            if (keyData == (Keys.Alt | Keys.P))
            {
                //do something
            }
            return false;
        }

你可能感兴趣的:(winform)