-1

I have put this code in my application, I want the window to hide when control + Q is clicked, why does this not work?

        private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.Q)
        {
            this.Hide();
        }
    }
Raghav J.
  • 53
  • 8
  • The code is needed for this? I just want to know how to do it. I don't want any tweaking in my code. – Raghav J. Aug 16 '15 at 10:21
  • Where do you want to capture `Ctrl+Q` when the window is closed? In another application window or do you want to register it as a global shortcut? – Damir Arh Aug 16 '15 at 10:28
  • Solutions to the question may be too broad which might lead to discussion from users and this site is not a discussion forum.. You better might have luck in other stackexchange site like programmers. In order to be more specific post your code so other might help on your code.. else try another site. – TFrost Aug 16 '15 at 10:29
  • No, I want it so when the window is open, if the user inputs Ctrl+Q it will hide the window until they click CTRL+Q again. – Raghav J. Aug 16 '15 at 10:30
  • 1
    @TFrost Programmers.SE is not a discussion forum either; over there it'd be off-topic as well as too broad. – Ixrec Aug 16 '15 at 10:42
  • @lxrec : sorry my mad.. I meant to say meta... – TFrost Aug 16 '15 at 10:46
  • Is `Form1_KeyDown` registered as a KeyDown event on Form1? – FlyingFoX Aug 16 '15 at 11:33
  • @FlyingFoX no it is not. How do I do that? Sorry I'm a newbie! – Raghav J. Aug 16 '15 at 11:49
  • I assume that you are using Windows Forms and Visual Studio. To check if your method is registered as an event open Form1 in the designer, go to properties, click on the event tab and look for the KeyDown event. If everything is fine you should see `Form1_KeyDown` there. – FlyingFoX Aug 16 '15 at 11:52

1 Answers1

0

You can check the Key modifier in order to handle Keys combination

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
  {
    //do stuff
   }
}
Slashy
  • 1,841
  • 3
  • 23
  • 42