开发者

How to get a combination of keys in c#

开发者 https://www.devze.com 2023-01-07 10:12 出处:网络
How 开发者_开发百科can I capture Ctrl + Alt + K + P keys on a C# form? thanksIt is a chord, you cannot detect it without memorizing having seen the first keystroke of the chord.This works:

How 开发者_开发百科can I capture Ctrl + Alt + K + P keys on a C# form? thanks


It is a chord, you cannot detect it without memorizing having seen the first keystroke of the chord. This works:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    private bool prefixSeen;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (prefixSeen) {
            if (keyData == (Keys.Alt | Keys.Control | Keys.P)) {
                MessageBox.Show("Got it!");
            }
            prefixSeen = false;
            return true;
        }
        if (keyData == (Keys.Alt | Keys.Control | Keys.K)) {
            prefixSeen = true;
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}


I'm not sure if you can. What you CAN do however, is the way Visual Studio does it. It has shortcuts like Ctrl + K, C. You first press Ctrl+K, then hold down Ctrl and press C. In your case, you could check for Ctrl+Alt+K,P.

You can first check for only Ctrl+Alt+K as done in the other answers, then set a member variable/flag to indicate Ctrl+Alt+K has been pressed. In the same method where you check for K you can check for P, and if the flag you just set was set to true, do whatever you need to do. Otherwise set the flag back to false.

Rough pseudo-code:

private bool m_bCtrlAltKPressed = false;

public void KeyDown() {
  if (Ctrl+Alt+K)
  {
    m_bCtrlAltKPressed = true;
  }
  else if (Ctrl+Alt+P && m_bCtrlAltKPressed) {
    //do stuff
  }
  else {
    m_bCtrlAltKPressed = false;
  }
}

Hope that's clear enough.


MessageFilters can help you in this case.

    public class KeystrokMessageFilter : System.Windows.Forms.IMessageFilter
    {
        public KeystrokMessageFilter() { }

        #region Implementation of IMessageFilter

        public bool PreFilterMessage(ref Message m)
        {
            if ((m.Msg == 256 /*0x0100*/))
            {
                switch (((int)m.WParam) | ((int)Control.ModifierKeys))
                {
                    case (int)(Keys.Control | Keys.Alt | Keys.K):
                        MessageBox.Show("You pressed ctrl + alt + k");
                        break;
                    //This does not work. It seems you can only check single character along with CTRL and ALT.
                    //case (int)(Keys.Control | Keys.Alt | Keys.K | Keys.P):
                    //    MessageBox.Show("You pressed ctrl + alt + k + p");
                    //    break;
                    case (int)(Keys.Control | Keys.C): MessageBox.Show("You pressed ctrl+c");
                        break;
                    case (int)(Keys.Control | Keys.V): MessageBox.Show("You pressed ctrl+v");
                        break;
                    case (int)Keys.Up: MessageBox.Show("You pressed up");
                        break;
                }
            }
            return false;
        }

        #endregion


    }

Now in your C# WindowsForm, register the MessageFilter for capturing key-strokes and combinations.

public partial class Form1 : Form
{
    KeystrokMessageFilter keyStrokeMessageFilter = new KeystrokMessageFilter();

    public Form1()
    {
        InitializeComponent();
    }       
    private void Form1_Load(object sender, EventArgs e)
    {
        Application.AddMessageFilter(keyStrokeMessageFilter);
    }
}

Somehow it only detects Ctrl + Alt + K. Please Read the comment in MessageFilter code.


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);
    }
}


See this great blog post about setting up hotkeys in c#

  • Christian Liensberger » Blog » Installing a global hot key with C#

There's also a good article that explains all of this here

  • Simple steps to enable Hotkey and ShortcutInput user control - CodeProject
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号