C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: Our solution involves the KeyDown event in our C# Windows Form and the KeyCode property.
Then: In the event tab, scroll to KeyDown, and double click in the space to the right. A new block of code will appear.
Example that uses KeyDown event handler: C#
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Linq;
namespace WindowsProgramNamespace
{
public partial class TextWindow : Form
{
public TextWindow()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
KeyDown implementation: C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Enter (return) was pressed.
// ... Call a custom method when user presses this key.
AcceptMethod();
}
}
Keys.Shift: Indicates the shift key was pressed. You could use this to prevent a user from typing uppercase letters.
Keys.Tab: Could use this to prevent user from tabbing out of a TextBox that isn't valid.
Keys.OemPipe, Keys.Oem: Some of these are apparently original equipment manufacturer specific. They may vary on the keyboard being used.
Keys.NumPad: Useful for data entry applications. You could setup the program to detect whether the numeric pad is being used.
Note: If you need to detect Shift, Tab, or an Arrow key, the e.KeyCode property can be ideal.