1

my question is how to detect two hot keys in win form application CTRL +C,CTRL,K like visual studio commenting command

I need to simulate VS hot key For commenting a line of code

mohammed sameeh
  • 4,711
  • 3
  • 18
  • 19

4 Answers4

1
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
   {

   }
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
1

Simple way is... There is a Windows API Function called ProcessCmdKey, by overriding this function we can achieve what we want

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
   if (keyData == (Keys.Control | Keys.C)) {
      MessageBox.Show("You have pressed the shortcut Ctrl+C");
      return true;
   }
   return base.ProcessCmdKey(ref msg, keyData);
}

Microsoft Documentation can be found here

source

Community
  • 1
  • 1
Kirk
  • 4,957
  • 2
  • 32
  • 59
  • Please give attribution when you pull an answer from another [question](http://stackoverflow.com/questions/4596600/cant-detect-a-ctrl-key-shortcut-on-keydown-events-whenever-theres-a-readonly/4596641#4596641). – Matt Davis Dec 25 '13 at 16:39
  • 1
    Actually i gave the link to the original post. your like is a 2nd dupe – Kirk Dec 25 '13 at 16:42
1
private bool _isFirstKeyPressedW = false;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control & e.KeyCode == Keys.W) 
    {
        _isFirstKeyPressedW = true;
    }
    if (_isFirstKeyPressedW) 
    {
        if (e.Control & e.KeyCode == Keys.S) 
        {
            //write your code 
        } 
        else 
        {
            _isFirstKeyPressedW = e.KeyCode == Keys.W;
        }
    }
}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
mohammed sameeh
  • 4,711
  • 3
  • 18
  • 19
0

if you want to handle Ctrl + C followed by Ctrl+ K you need to maintain a state variable.

Set the Form KeyPreview property to true and handle the Form KeyDown event.

Try This:

   this.KeyPreview=true;
   private bool isFirstKeyPressed= false;
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.C)
        {               
            isFirstKeyPressed = true;                
        }


        if (isFirstKeyPressed)
        {
            if (e.Control && e.KeyCode == Keys.K)
            {
                MessageBox.Show("Ctrl+C and Ctrl+K pressed Sequentially");
                /*write your code here*/
                isFirstKeyPressed= false;
            }
        }
    }
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • `isFirstKeyPressed` ought to be reset to false in *every* case after it is set to true, right? – Matt Davis Dec 25 '13 at 17:04
  • yes it has to be but if user press `Ctrl+C` , `Ctrl` and before pressing the `K` itself `isFirstKeyPressed` becomes false. – Sudhakar Tillapudi Dec 25 '13 at 17:11
  • @SudhakarTillapudi your code is right but it miss an important thing if you press (CTRL+C) then followed by any random character and then (CTRL+W) will be executed i will post the code , thanks – mohammed sameeh Dec 30 '13 at 17:17