Displaying information about the key the user pressed

image_pdfimage_print


   


   using System;
   using System.Drawing;
   using System.Collections;
   using System.ComponentModel;
   using System.Windows.Forms;
   using System.Data;

   public class KeyDemo : System.Windows.Forms.Form
   {
      private System.Windows.Forms.Label charLabel;
      private System.Windows.Forms.Label keyInfoLabel;
      public KeyDemo()
      {
         InitializeComponent();
      }

      private void InitializeComponent()
      {
         this.charLabel = new System.Windows.Forms.Label();
         this.keyInfoLabel = new System.Windows.Forms.Label();
         this.SuspendLayout();

         this.charLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
         this.charLabel.Location = new System.Drawing.Point(8, 8);
         this.charLabel.Name = "charLabel";
         this.charLabel.Size = new System.Drawing.Size(168, 32);
         this.charLabel.TabIndex = 0;

         this.keyInfoLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
         this.keyInfoLabel.Location = new System.Drawing.Point(8, 56);
         this.keyInfoLabel.Name = "keyInfoLabel";
         this.keyInfoLabel.Size = new System.Drawing.Size(168, 136);
         this.keyInfoLabel.TabIndex = 0;

         this.AutoScaleBaseSize = new System.Drawing.Size(15, 37);
         this.ClientSize = new System.Drawing.Size(184, 197);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {this.keyInfoLabel,this.charLabel});
         this.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F);
         this.Name = "Key Demo";
         this.Text = "Key Demo";

         this.KeyDown +=new System.Windows.Forms.KeyEventHandler(this.KeyDemo_KeyDown );
         this.KeyPress +=new System.Windows.Forms.KeyPressEventHandler(this.KeyDemo_KeyPress );
         this.KeyUp +=new System.Windows.Forms.KeyEventHandler(this.KeyDemo_KeyUp );
         
         this.ResumeLayout(false);

      }
      [STAThread]
      static void Main() 
      {
         Application.Run( new KeyDemo() );
      }

      protected void KeyDemo_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e )
      {  
         charLabel.Text = "Key pressed: " + e.KeyChar;
      }

      private void KeyDemo_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e )
      {
         keyInfoLabel.Text = 
            "Alt: " + (e.Alt ? "Yes" : "No") + '
' +
            "Shift: " + (e.Shift ? "Yes" : "No" ) + '
' +
            "Ctrl: " + (e.Control ? "Yes" : "No" ) + '
' + 
            "KeyCode: " + e.KeyCode + '
' +
            "KeyData: " + e.KeyData + '
' +
            "KeyValue: " + e.KeyValue;                               
      }
   
      private void KeyDemo_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e )
      {
        Console.WriteLine("Key up");
      }

   }

           
          


This entry was posted in Event. Bookmark the permalink.