C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
KeyEventArgs: In the Window_KeyDown method, the KeyEventArgs is important. From it, we access the key that was pressed.
Key: This is the property that tells us what key was pressed. In this example, we test against the Key.F5 constant.
Tip: When the user presses F5, the window title will change to a special message. In many programs, like web browsers, F5 means "reload."
Example markup: XAML
<Window x:Class="WpfApplication25.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
KeyDown="Window_KeyDown">
</Window>
Example code: C#
using System.Windows;
using System.Windows.Input;
namespace WpfApplication25
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// ... Test for F5 key.
if (e.Key == Key.F5)
{
this.Title = "You pressed F5";
}
}
}
}