-2

I'm working on WPF application, and I'm showing modal/form when Key Combination is pressed, so in my case it is CTRL + F9, so here is my code:

//Listening on Window_PreviewKeyDown any key pressing
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{ 

 if (e.Key == Key.Escape)
 {
    LockAllInputs();
 }

 if (e.Key == Key.Tab)
 {
    e.Handled = true;
 }

 if (e.Key == Key.F11)
 {
     this.Close();
 }
   // Opening modal when Key combination of CTRL AND F9 is pressed
   if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
   {
     MyModal modal = new MyModal();
    // Do something
   }

   //Hitting a method when only F9 is pressed
    if (e.Key == Key.F9)
    {
      //This is invoked everytime after CTRL+F9
      CallAnotherMethod();
    }
}

But the issue in my code is that, when I hit CTRL+F9 it works fine, but after that method that is invoked when F9 is pressed is being invoked also.. and that is something I want to avoid, CTRL+F9 is doing one thing, F9 is doing some another thing, so I dont want F9 to be invoked when CTRL+F9 is pressed...

Thanks guys

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102

3 Answers3

1
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
{
  MyModal modal = new MyModal();
  modal.ShowDialog();
  e.Handled = true;//Here I've tried to prevent hitting another method which is being called when F9 is pressed
 }

//Hitting a method when only F9 is pressed
if (e.Key == Key.F9)
{
  //This is invoked everytime after CTRL+F9
  CallAnotherMethod();
}

Your code will continue to execute after the first if, thus will enter the second if as well.

The simplest solution would be to change the second if to an else if:

else if (e.Key == Key.F9)
{
  //This is invoked everytime after CTRL+F9
  CallAnotherMethod();
}

Another option would be to stop executing the function inside the first if:

if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
{
  MyModal modal = new MyModal();
  modal.ShowDialog();
  e.Handled = true;
  return;
}
mjwills
  • 23,389
  • 6
  • 40
  • 63
  • I dont need e.handled there ? – Roxy'Pro Dec 27 '17 at 13:01
  • I am not sure @Roxy'Pro - try removing it and see what happens. _I left the core structure of your code unchanged._ – mjwills Dec 27 '17 at 13:02
  • Both solutions are wrong. They will pass if someone presses Shift + F9 for example... Whatever @Roxy'Pro accepted this as answer so I guess he knows what he has been asking for... – Ivan Ičin Dec 27 '17 at 13:15
  • @IvanIčin I asked you something in a comment below your question, can you please answer me. – Roxy'Pro Dec 27 '17 at 13:52
  • Can you point me to the exact question you are referring to? I appear to have responded to both of them @Roxy'Pro. – mjwills Dec 27 '17 at 21:14
1

This is how it should be:

    //Listening on Window_PreviewKeyDown any key pressing
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{ 

 if (e.Key == Key.Escape)
 {
    LockAllInputs();
 }

 if (e.Key == Key.Tab)
 {
    e.Handled = true;
 }

 if (e.Key == Key.F11)
 {
     this.Close();
 }
   // Opening modal when Key combination of CTRL AND F9 is pressed
   if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
   {
     MyModal modal = new MyModal();
     modal.ShowDialog();
     e.Handled = true;//Here I've tried to prevent hitting another method which is being called when F9 is pressed
   }

   //Hitting a method when only F9 is pressed
    if (Keyboard.Modifiers == ModifierKeys.None && e.Key == Key.F9)
    {
      CallAnotherMethod();
    }
}
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • Thanks Ivan, but I'm wondering why this code is not working when if (Keyboard.Modifiers == ModifierKeys.None && e.Key == Key.F9) is moved above first if condition (It looks like it MUST be below.. but why) – Roxy'Pro Dec 27 '17 at 13:10
  • @Roxy'Pro I think you've got something wrong, try it again. It could be as trivial as that you have pressed the wrong key while testing. – Ivan Ičin Dec 27 '17 at 14:12
  • //Please test your code, because when I tested it, after pressing CTRL+F9 first if condition is satisfied and it's running into second one (Keyboard.modifiers none) which is not an option – Roxy'Pro Dec 28 '17 at 08:35
  • @Roxy'Pro this works. You probably use debugger and then drop the ctrl key while using debugger and then the second if can pass. As said this works, whenever you say that this doesn't work try to figure out what mistake you made while testing. – Ivan Ičin Dec 28 '17 at 12:33
0

You are only checking if the F9 key is the one that triggered the event, you're not checking if any other keys are also pressed. You have 2 solutions:

  1. Change the second if to an if else;
  2. Check if the other modifiers are not pressed.
Magnetron
  • 7,495
  • 1
  • 25
  • 41