Wpf 当按键“0”时调用函数;“空间”;被压在窗户里
我正在开发一个棋盘游戏,我想在按下窗口中的空格时调用一个函数,但在搜索过程中找不到问题的答案。Wpf 当按键“0”时调用函数;“空间”;被压在窗户里,wpf,Wpf,我正在开发一个棋盘游戏,我想在按下窗口中的空格时调用一个函数,但在搜索过程中找不到问题的答案。 你有什么想法吗?有很多方法可以做到这一点。最简单的方法是连接一个事件处理程序(尽管不是最优雅的)。不过,也可以使用涉及命令的更优雅的解决方案,具体取决于您使用的控件 XAML XAML: <Window KeyDown="MainWindow_OnKeyDown" <!-- other properties --> > <!-- rest of y
你有什么想法吗?有很多方法可以做到这一点。最简单的方法是连接一个事件处理程序(尽管不是最优雅的)。不过,也可以使用涉及命令的更优雅的解决方案,具体取决于您使用的控件 XAML XAML:
<Window KeyDown="MainWindow_OnKeyDown"
<!-- other properties -->
>
<!-- rest of your UI -->
</Window>
private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
// do something
}
}
<Window ...
PreviewKeyUp="Window_PreviewKeyUp" >
</Window>
// Use the PreviewKeyUp event to capture the keypress before
// any child controls that have focus handle the event
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
if(e.Key == Key.Space)
{
AFunction();
// to prevent the key press to bubble up to child controls that have focus
e.Handled = true;
}
}