views:

69

answers:

4

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

+1  A: 

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

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

Codesleuth
A: 

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.

this. __curious_geek
+2  A: 

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. xD

horsedrowner
+2  A: 

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