TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to WPF

WPF KeyDown Event Handler

Understand the WPF KeyDown and KeyUp event handlers. Test keys with the Key property.
KeyDown. Applications sometimes need to access keyboard commands. This helps usability. With the KeyDown event handler, we can listen for key presses, like "function keys" like F5 for example.Controls
Example. Let us begin with a new WPF program. Please add a KeyDown event handler to the Window element. Let Visual Studio create Window_KeyDown by pressing Enter. Then, add some code to the event handler.

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"; } } } }
KeyBinding. More complicated key presses are handled with "gestures" in WPF. We can specify commands with KeyBinding elements. On MenuItems, we can specify InputGestureText. With a simple KeyDown method, we cannot easily handle complex commands.Menu
Summary. With KeyDown, we can implement simple keyboard commands in WPF programs. We used the Key property to test a KeyEventsArg object. Often the simplest solution is best. But for more complex requirements, like gestures, KeyBindings are needed.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf