导致调用方法的组合键 (CTRL + F9) 应仅在按下 F9 时调用
Key combination (CTRL + F9) causing calling method which should be called only when F9 is pressed
我正在开发 WPF 应用程序,当按下组合键时我会显示 modal/form,所以在我的例子中是 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 正在做另一件事,所以我不希望在按下 CTRL+F9
时调用 F9
...
谢谢大家
您只是在检查 F9 键是否是触发事件的键,而不是在检查是否还按下了任何其他键。您有 2 个解决方案:
- 将第二个
if
更改为 if else
;
- 检查是否没有按下其他修饰符。
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
之后继续执行 ,因此也将进入第二个 if
。
最简单的解决方案是将第二个 if
更改为 else if
:
else if (e.Key == Key.F9)
{
//This is invoked everytime after CTRL+F9
CallAnotherMethod();
}
另一种选择是停止执行函数 inside 第一个 if
:
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
{
MyModal modal = new MyModal();
modal.ShowDialog();
e.Handled = true;
return;
}
应该是这样的:
//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();
}
}
我正在开发 WPF 应用程序,当按下组合键时我会显示 modal/form,所以在我的例子中是 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 正在做另一件事,所以我不希望在按下 CTRL+F9
时调用 F9
...
谢谢大家
您只是在检查 F9 键是否是触发事件的键,而不是在检查是否还按下了任何其他键。您有 2 个解决方案:
- 将第二个
if
更改为if else
; - 检查是否没有按下其他修饰符。
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
之后继续执行 ,因此也将进入第二个 if
。
最简单的解决方案是将第二个 if
更改为 else if
:
else if (e.Key == Key.F9)
{
//This is invoked everytime after CTRL+F9
CallAnotherMethod();
}
另一种选择是停止执行函数 inside 第一个 if
:
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F9)
{
MyModal modal = new MyModal();
modal.ShowDialog();
e.Handled = true;
return;
}
应该是这样的:
//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();
}
}