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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Event » Key EventScreenshots 
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(88);
         this.charLabel.Name = "charLabel";
         this.charLabel.Size = new System.Drawing.Size(16832);
         this.charLabel.TabIndex = 0;

         this.keyInfoLabel.Font = new System.Drawing.Font("Microsoft Sans Serif"12F);
         this.keyInfoLabel.Location = new System.Drawing.Point(856);
         this.keyInfoLabel.Name = "keyInfoLabel";
         this.keyInfoLabel.Size = new System.Drawing.Size(168136);
         this.keyInfoLabel.TabIndex = 0;

         this.AutoScaleBaseSize = new System.Drawing.Size(1537);
         this.ClientSize = new System.Drawing.Size(184197);
         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.Runnew 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.