键组合(CTRL + F9)导致调用方法,只有在按下F9时才应调用该方法

时间:2021-08-10 18:22:06

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:

我正在使用WPF应用程序,并且在按下组合键时我显示模态/表单,所以在我的情况下它是CTRL + F9,所以这是我的代码:

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

但是我的代码中的问题是,当我按下CTRL + F9时它工作正常,但是在按下F9时调用的方法也被调用..这是我想要避免的,CTRL + F9正在做一件事,F9正在做另外一件事,所以我不想在按下CTRL + F9时调用F9 ...

Thanks guys

3 个解决方案

#1


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.

您的代码将在第一个if之后继续执行,因此也将输入第二个。

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

最简单的解决方案是将第二个if更改为else,如果:

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;
}

#2


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();
    }
}

#3


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:

您只检查F9键是否是触发事件的键,您不检查是否还按下任何其他键。你有2个解决方案:

  1. Change the second if to an if else;
  2. 将第二个if更改为if else;

  3. Check if the other modifiers are not pressed.
  4. 检查是否未按下其他修改器。

#1


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.

您的代码将在第一个if之后继续执行,因此也将输入第二个。

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

最简单的解决方案是将第二个if更改为else,如果:

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;
}

#2


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();
    }
}

#3


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:

您只检查F9键是否是触发事件的键,您不检查是否还按下任何其他键。你有2个解决方案:

  1. Change the second if to an if else;
  2. 将第二个if更改为if else;

  3. Check if the other modifiers are not pressed.
  4. 检查是否未按下其他修改器。