Key event: key down, key up, key pressed : KeyEvent « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » KeyEvent 
23.61.2.Key event: key down, key up, key pressed
Key event: key down, key up, key pressed
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class KeyEventDetailInfo : System.Windows.Forms.Form
{
  private System.Windows.Forms.TextBox txtInput;
  private System.Windows.Forms.Label lblUpper;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label lblLower;
  private System.Windows.Forms.Button btnReset;

  public KeyEventDetailInfo()
  {
    InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.txtInput = new System.Windows.Forms.TextBox();
    this.btnReset = new System.Windows.Forms.Button();
    this.lblUpper = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label1 = new System.Windows.Forms.Label();
    this.lblLower = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // txtInput
    // 
    this.txtInput.Location = new System.Drawing.Point(88);
    this.txtInput.Name = "txtInput";
    this.txtInput.TabIndex = 0;
    this.txtInput.Text = "";
    this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
    this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
    this.txtInput.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyUp);
    // 
    // btnReset
    // 
    this.btnReset.Location = new System.Drawing.Point(3288);
    this.btnReset.Name = "btnReset";
    this.btnReset.TabIndex = 2;
    this.btnReset.Text = "Reset";
    this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
    // 
    // lblUpper
    // 
    this.lblUpper.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblUpper.Location = new System.Drawing.Point(36856);
    this.lblUpper.Name = "lblUpper";
    this.lblUpper.Size = new System.Drawing.Size(3223);
    this.lblUpper.TabIndex = 3;
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(32056);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(4016);
    this.label2.TabIndex = 4;
    this.label2.Text = "Upper:";
    this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(320104);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(4016);
    this.label1.TabIndex = 5;
    this.label1.Text = "Lower:";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    // 
    // lblLower
    // 
    this.lblLower.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblLower.Location = new System.Drawing.Point(368104);
    this.lblLower.Name = "lblLower";
    this.lblLower.Size = new System.Drawing.Size(3223);
    this.lblLower.TabIndex = 6;
    // 
    // KeyEventDetailInfo
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(417293);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                             this.lblLower,
                                             this.label1,
                                             this.label2,
                                             this.lblUpper,
                                             this.btnReset,
                                             this.txtInput});
    this.ResumeLayout(false);

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

  private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    Console.WriteLine("KeyCode name: " + e.KeyCode);
    Console.WriteLine("KeyCode key code: " ((int)e.KeyCode));
    Console.WriteLine("KeyData name: " + e.KeyData);
    Console.WriteLine("KeyData key code: " ((int)e.KeyData));
    Console.WriteLine("KeyValue: " + e.KeyValue);
    Console.WriteLine("Handled: " + e.Handled);
  }

  private void txtInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
    char keyChar;
    keyChar = e.KeyChar;

    Console.WriteLine("KeyPress event.");
    Console.WriteLine("KeyChar: " + keyChar);
    Console.WriteLine("KeyChar Code: " (int)keyChar);
    Console.WriteLine("Handled: " + e.Handled);

    lblUpper.Text = keyChar.ToString().ToUpper();
    lblLower.Text = keyChar.ToString().ToLower();
  
    if (keyChar.ToString() == "$")
    {
      txtInput.AppendText("#");
      e.Handled = true;
    }
  }

  private void txtInput_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    Console.WriteLine("KeyCode name: " + e.KeyCode);
    Console.WriteLine("KeyCode key code: " ((int)e.KeyCode));
    Console.WriteLine("KeyData name: " + e.KeyData);
    Console.WriteLine("KeyData key code: " ((int)e.KeyData));
    Console.WriteLine("KeyValue: " + e.KeyValue);
    Console.WriteLine("Handled: " + e.Handled);
  }

  private void btnReset_Click(object sender, System.EventArgs e)
  {
    txtInput.Text = "";
    lblUpper.Text = "";
    lblLower.Text = "";
  }
}
23.61.KeyEvent
23.61.1.KeyEvent: key char(code)KeyEvent: key char(code)
23.61.2.Key event: key down, key up, key pressedKey event: key down, key up, key pressed
23.61.3.Change key event (key code) during in the event handlerChange key event (key code) during in the event handler
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.