2016-03-06 97 views
-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(); 
    } 
} 

希望这应该清除任何问题,对此不清楚抱歉。

+1

你为什么用WPF的Windows Forms'MessageBox'? – poke

+0

重复的问题,已经在回答:http://stackoverflow.com/questions/7691713/how-to-close-a-window-in-wpf-on-a-escape-key –

+0

我不知道,最简单的方法我知道显示一个弹出消息,这真的很重要吗? – user1768788

回答

0

也许WPF的MessageBox相关

你有另一个控制是可能处理事件这只是正常的我

<Window PreviewKeyDown="wPreviewKeyDown" 

private void wPreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    MessageBox.Show(e.Key.ToString()); 
    if (e.Key == Key.Escape) 
     this.Close(); 
} 

+0

请参阅编辑,谢谢你 – user1768788