Mega Code Archive

 
Categories / C# / WPF
 

Close a window with Escape key pressed

<Window x:Class="_360Timer.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Concentric Rings" Width="910" Height="512">   <Canvas Name="MainCanvas" Background="#FFE0E0E0"/> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Media.Animation; namespace _360Timer {     public partial class Window1 : Window     {         public Window1()         {             InitializeComponent();             this.Show();             this.KeyDown += new System.Windows.Input.KeyEventHandler(Window1_KeyDown);         }         void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)         {             if (e.Key == System.Windows.Input.Key.Escape)                 this.Close();         }     } }