代码之家  ›  专栏  ›  技术社区  ›  Roxy'Pro

组合键(CTRL+F9)导致调用方法,仅当按F9时才应调用该方法

  •  -2
  • Roxy'Pro  · 技术社区  · 7 年前

    我正在处理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();
        }
    }
    

    但我代码中的问题是 CTRL+F9 它工作正常,但在调用该方法之后 F9 也正在调用。。 这是我想要避免的, CTRL+F9 正在做一件事,F9正在做另一件事,所以我不想 F9层 在以下情况下调用 CTRL+F9 已按下。。。

    谢谢各位

    3 回复  |  直到 7 年前
        1
  •  1
  •   mjwills Myles McDonnell    7 年前
    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();
    }
    

    您的代码将继续执行 之后 第一个 if ,则将进入第二个 如果

    最简单的解决方案是改变第二个 如果 else if :

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

    另一种选择是停止执行该函数 在…内 第一个 如果 :

    if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
    {
      MyModal modal = new MyModal();
      modal.ShowDialog();
      e.Handled = true;
      return;
    }
    
        2
  •  1
  •   Ivan Ičin    7 年前

    应该是这样的:

        //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
  •   Magnetron    7 年前

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

    1. 更改第二个 if if else ;
    2. 检查是否未按下其他修改器。