Displaying information about the key the user pressed : Key Event « Event « C# / C Sharp






Displaying information about the key the user pressed

Displaying information about the key the user pressed

   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") + '\n' +
            "Shift: " + (e.Shift ? "Yes" : "No" ) + '\n' +
            "Ctrl: " + (e.Control ? "Yes" : "No" ) + '\n' + 
            "KeyCode: " + e.KeyCode + '\n' +
            "KeyData: " + e.KeyData + '\n' +
            "KeyValue: " + e.KeyValue;                               
      }
   
      private void KeyDemo_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e )
      {
        Console.WriteLine("Key up");
      }

   }

           
       








Related examples in the same category

1.Event system explained in detail
2.Close Form with Pressing X key
3.Check KeyCode from KeyEventArgs
4.Shift key pressedShift key pressed
5.Control Key pressedControl Key pressed
6.Alt key pressedAlt key pressed
7.Get Async Key StateGet Async Key State
8.Get Key action informationGet Key action information
9.Displays a key pressed by the userDisplays a key pressed by the user
10.Full screen and KeyEvent and MouseEvent
11.Key PressKey Press
12.Key Timer Key Timer
13.Keyboard SampleKeyboard Sample