-1
XAML文件WPF keydown无法捕捉逃生?
PreviewKeyDown="Window_KeyDown"
.cs文件
private void Window_KeyDown(object sender, KeyEventArgs e)
{
System.Windows.Forms.MessageBox.Show(e.Key.ToString());
if (e.Key == Key.Escape)
{
this.Close();
}
}
的KeyPreview设置为true,虽然消息框不会出现ESC键。 (消息框显示为其他“常规”键,如0-9和a-z)。我如何解决这个问题或找到一种方法来触发ESC上的某些东西?
EDIT
Win.xaml
<Window x:Class="WindowsFormsApplication5.Win"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
PreviewKeyDown="Window_KeyDown">
</Window>
Win.xaml.cs
public partial class Win : Window
{
public Win()
{
InitializeComponent();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
System.Windows.MessageBox.Show(e.Key.ToString());
if (e.Key == Key.Escape)
{
this.Close();
}
}
}
Form1.cs的
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Win scrn = new Win();
scrn.Show();
}
}
希望这应该清除任何问题,对此不清楚抱歉。
你为什么用WPF的Windows Forms'MessageBox'? – poke
重复的问题,已经在回答:http://stackoverflow.com/questions/7691713/how-to-close-a-window-in-wpf-on-a-escape-key –
我不知道,最简单的方法我知道显示一个弹出消息,这真的很重要吗? – user1768788