Get the Pressed Key using C#

In this tutorial, i will teach you how to create a program that will capture, obtain, or get a key pressed by the user. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Get the Pressed Key. We don't need to design an interface because we have to get only the pressed key by the user. 2. Now put this code for your code module. We choose the Form control and its Keypress event to have the pressed key obtained. Keypress event occurs when the user presses and releases a key. You can use the KeyPress event for intercepting keystrokes entered in a control.
  1. public void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
We will have to create an If Statement and Strings.Asc(e.KeyChar) > 1 which means that there is a pressed key done by the user. Asc Function returns an integer representing the character code corresponding to the first letter in a string. Variable e was used as a form of keychar as this variable is a variable for KeyPress Events arguments.
  1. if (Strings.Asc(e.KeyChar) > 1)
  2.                         {
Then, display the message to get the pressed key by using e.KeyChar. e.KeyChar gets or sets the character corresponding to the key pressed.
  1. MessageBox.Show("You have pressed " + e.KeyChar + " key");
Keep e.Handled to be equal to True. e.Handled gets or sets a value indicating whether the event was handled and returns boolean.
  1. e.Handled = true;
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10.  
  11. namespace Get_the_pressed_key
  12. {
  13.         public partial class Form1
  14.         {
  15.                 public Form1()
  16.                 {
  17.                         InitializeComponent();
  18.                        
  19.                        
  20.                 }
  21.  
  22.                 public void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  23.                 {
  24.                         if (Strings.Asc(e.KeyChar) > 1)
  25.                         {
  26.                                 MessageBox.Show("You have pressed " + e.KeyChar + " key");
  27.                                 e.Handled = true;
  28.                         }
  29.                 }
  30.  
  31.  
  32.         }
  33.        
  34. }
Output: output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment