How to get a combination of keys in c#

后端 未结 5 1314
旧巷少年郎
旧巷少年郎 2020-11-29 10:12

How can I capture Ctrl + Alt + K + P keys on a C# form? thanks

5条回答
  •  -上瘾入骨i
    2020-11-29 10:53

    You can use GetKeyState to get the status of the other keys when one of the keys in the combination has been pressed. Here's an example on a form.

    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace DetectKeyChord
    {
        public partial class Form1 : Form
        {
            private const int WM_KEYDOWN = 0x100;
            private const int KEY_PRESSED = 0x80;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            public void ShortcutAction()
            {
                MessageBox.Show("Ctrl+Alt+K+P has been pressed.");
            }
    
            private bool IsKeyDown(Keys key)
            {
                return (NativeMethods.GetKeyState(key) & KEY_PRESSED) == KEY_PRESSED;
            }
    
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_KEYDOWN)
                {
                    //If any of the keys in the chord have been pressed, check to see if
                    //the entire chord is down.
                    if (new[] { Keys.P, Keys.K, Keys.ControlKey, Keys.Menu }.Contains((Keys)m.WParam))
                    {
                        if (IsKeyDown(Keys.ControlKey) && IsKeyDown(Keys.Menu) && IsKeyDown(Keys.K) && IsKeyDown(Keys.P))
                        {
                            this.ShortcutAction();
                        }
                    }
                }
                base.WndProc(ref m);
            }
        }
    
        public static class NativeMethods
        {
            [DllImport("USER32.dll")]
            public static extern short GetKeyState(Keys nVirtKey);
        }
    }
    

提交回复
热议问题