如何检测 WPF 中按下的键?
How to detect a pressed key in WPF?
我用 WPF 编写了一个带有几个按钮的桌面应用程序。它到目前为止有效,但现在我想 link 按钮到键。我希望程序检测是否按下了按钮,然后执行 Button_click 方法。我试图通过添加此方法来实现此目的
public void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
Button0_Click(new object(), new RoutedEventArgs());
break;
case Key.D1:
Button1_Click(new object(), new RoutedEventArgs());
break;
case Key.D2:
Button2_Click(new object(), new RoutedEventArgs());
break;
case Key.D3:
Button2_Click(new object(), new RoutedEventArgs());
break;
case Key.D4:
Button4_Click(new object(), new RoutedEventArgs());
break;
}
}
但是根本没有用。我怎样才能 link 定义按键方法到按键?
您需要实施 key event handler
例如对于键 Enter
Refer to Microsoft docs here
XAML代码
<StackPanel>
<TextBlock Width="300" Height="20">
Type some text into the TextBox and press the Enter key.
</TextBlock>
<TextBox Width="300" Height="30" Name="textBox1"
KeyDown="OnKeyDownHandler"/>
<TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>
后端的关键事件处理程序
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//do your things here
}
}
在加载主要 window 时创建一个事件:
在 XAML 中:
Loaded="MainWindow_Loaded"
在后面的代码中:
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var window = Window.GetWindow(this);
window.KeyDown += HandleKeyPress;
}
处理您的按键事件:
private void HandleKeyPress(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
Button0_Click(new object(), new RoutedEventArgs());
break;
case Key.D1:
Button1_Click(new object(), new RoutedEventArgs());
break;
....
}
我用 WPF 编写了一个带有几个按钮的桌面应用程序。它到目前为止有效,但现在我想 link 按钮到键。我希望程序检测是否按下了按钮,然后执行 Button_click 方法。我试图通过添加此方法来实现此目的
public void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
Button0_Click(new object(), new RoutedEventArgs());
break;
case Key.D1:
Button1_Click(new object(), new RoutedEventArgs());
break;
case Key.D2:
Button2_Click(new object(), new RoutedEventArgs());
break;
case Key.D3:
Button2_Click(new object(), new RoutedEventArgs());
break;
case Key.D4:
Button4_Click(new object(), new RoutedEventArgs());
break;
}
}
但是根本没有用。我怎样才能 link 定义按键方法到按键?
您需要实施 key event handler
例如对于键 Enter
Refer to Microsoft docs here
XAML代码
<StackPanel>
<TextBlock Width="300" Height="20">
Type some text into the TextBox and press the Enter key.
</TextBlock>
<TextBox Width="300" Height="30" Name="textBox1"
KeyDown="OnKeyDownHandler"/>
<TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>
后端的关键事件处理程序
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//do your things here
}
}
在加载主要 window 时创建一个事件:
在 XAML 中:
Loaded="MainWindow_Loaded"
在后面的代码中:
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var window = Window.GetWindow(this);
window.KeyDown += HandleKeyPress;
}
处理您的按键事件:
private void HandleKeyPress(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
Button0_Click(new object(), new RoutedEventArgs());
break;
case Key.D1:
Button1_Click(new object(), new RoutedEventArgs());
break;
....
}