Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a combination of keys in c#

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

like image 383
Ristovak Avatar asked Jul 14 '10 07:07

Ristovak


People also ask

How do you do key combinations?

To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign.

Which shortcut key is combination?

ctrl+F4 is known as the combination key of the keyboard. A key combination is the use of two or more keys on a keyboard to generate a specific result. These keys are pressed either at the same time, or one after the other while holding down each key until the last key is pressed.

What is the shortcut keys of CTRL C?

Keyboard Command: Control (Ctrl) + C The COPY command is used for just that - it copies the text or image you have selected and stores is on your virtual clipboard, until it is overwritten by the next "cut" or "copy" command.

How many combinations are there in a key?

Answer: 1 key with no modifiers pressed. Therefore each key has a potential of 63 unique combinations with 6 modifiers, 64 if you include it on it's own.

What are combination keys on keyboard?

Combination Keys on Keyboard and their Functions; ☆☛✅ A full list of Combination Keys & definition. A key combination is the use of two or more keys on a keyboard to generate a specific result. These keys are pressed either at the same time, or one after the other while holding down each key until the last key is pressed.

How to find all combinations of numbers in C?

Take the numbers from the user. Store the numbers in an array of size 3. Using three for loops, find out all combinations of 0,1 and 3. Whenever a new combination is found, print out the content of the array for that specific index for each loop. The c program will look like as below : Create three integers i,j and k to use in the for loops.

How to generate all possible combinations out of a programming language?

C++ Program to Generate All Possible Combinations Out of a,b,c,d,e 1 Algorithms. Begin Take the number of elements and the elements as input. ... 2 Example 3 Output

How to list all possible key combinations defined by a keyboard filter?

You can list the value of the WEKF_PredefinedKey.Id to get a complete list of key combinations defined by a keyboard filter. You can use the values in the WEKF_PredefinedKey.Id column to configure the Windows Management Instrumentation (WMI) class WEKF_PredefinedKey.


4 Answers

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);
    }
}
like image 135
Hans Passant Avatar answered Oct 22 '22 10:10

Hans Passant


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.

like image 33
Vivelin Avatar answered Oct 22 '22 12:10

Vivelin


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.

like image 2
this. __curious_geek Avatar answered Oct 22 '22 10:10

this. __curious_geek


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);
    }
}
like image 2
transistor1 Avatar answered Oct 22 '22 10:10

transistor1