In depth analysis of the handling of keyboard related events in C

  • 2020-05-12 03:03:48
  • OfStack

There are relatively few keyboard-related events in C #, and there are roughly three types: "KeyDown, ""KeyUp," and "KeyPress."
(1). How to define these events in the C # program:
The Delegate of the events in C # that describe "KeyDown" and "KeyUp" is "KeyEventHandler". The Delegate used to describe "KeyPress" is "KeyPressEventHandler". Both Delegate are encapsulated in the named space "Syetem.Windows.Froms". The class that provides data for "KeyDown", "KeyUp" events is "KeyEventArgs". The class that provides the data for the "KeyPress" event is "KeyPressEventArgs". These two are also encapsulated in the named space "Syetem.Windows.Froms".
The syntax for defining "KeyDown" and "KeyUp" events in the C # program is as follows:
"Component name "." event name "+= new Syetem.Windows.Froms.KeyEventHandler (" event name");
The following is the specific implementation code in the program:

button1. KeyUp += new Syetem.Windows.Froms. KeyEventHandler ( button1_KUp );  

Here is the basic structure for responding to the above event.

private void button1_KUp ( object sender , Syetem.Windows.Froms. KeyEventArgs e )
{
 Add the code to respond to this event here 
} 
 in C Program definition "KeyPress" The syntax of the event is as follows: 
" Component name "." The name of the event "+= new Syetem.Windows.Froms. KeyPressEventHandler ( " The name of the event " ); 

The following is the specific implementation code in the program:

button1. KeyPress += new Syetem.Windows.Froms. KeyPressEventArgs ( button1_KPress );  

Once you've defined the event, you need to include code that responds to the event in your program, otherwise the program will report an error when it compiles. Here is the basic structure for responding to the above event.

private void button1_KPress ( object sender , Syetem.Windows.Froms. KeyPressEventArgs e )
{
 Add the code to respond to this event here 
} 
 Note: appears in the program "button1" Is to define the 1 Button components. 
(

(2). Handling methods of typical problems in keyboard-related events:
The typical problem with keyboards is to determine which keys are being pressed. You can do all three of the above. And in the "KeyEventArgs" class, a generic "KeyCode" is passed, which can be used to read the current key. So we deal with this in the case of "KeyUp" or "KeyDown". According to the above knowledge, you can get the program code written with C # to read and read the key. Here is the code (key.cs) and the interface after the code is run:
Figure 02: program interface for reading keyboard keys with C #
The code for key.cs is as follows:

 code  
 using System ;
  using System.Drawing ;
  using System.Collections ;
  using System.ComponentModel ;
  using System.Windows.Forms ;
  using System.Data ;
  public class Form1 : Form
 {
 private System.ComponentModel.Container components = null ;

 public Form1 ( )
 {
 file:// Initialize the various components in the form 
 InitializeComponent ( ) ;
 }
 protected override void Dispose ( bool disposing )
 {
 file:// Clear the resources used in the program 
 if ( disposing )
 {
 if ( components != null )
 {
 components.Dispose ( ) ;
 }
 }
 base.Dispose ( disposing ) ;
 }
 private void InitializeComponent ( )
 {
 this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
 this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
 this.Name = "Form1" ;
 this.Text = "C Handle keyboard events! " ;
 file:// Defines the action of a key 1 Event handling procedures "Form1_KeyUp"
 this.KeyUp += new KeyEventHandler ( this.Form1_KeyUp ) ;

 }
 static void Main ( )
 {
 Application.Run ( new Form1 ( ) ) ;
 }
 file:// Displays the name of the key you pressed 
 private void Form1_KeyUp ( object sender , KeyEventArgs e )
 {
 MessageBox.Show ( e.KeyCode.ToString ( ) , " The key you pressed is: " ) ;

 }
 }

  All of them are found on the Internet. After I read them, I tried many times, but the buttons didn't respond ......
  Later found in the book, the original   You have to put the corresponding one first form  the KeyPreview Attribute is set to true 
 C# codethis.KeyPreview=true;this.KeyDown+=newKeyEventHandler(frmNewBook_KeyDown); 

         }voidfrmNewBook_KeyDown(objectsender, KeyEventArgs e) 
         {switch(e.KeyData) 
             {caseKeys.F4: 
                     Console.WriteLine(" Give me some points ");break;//.........} 
         }


Related articles: