Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:22:36
C#教程:KeyDown与KeyUp事件使用实例

C#教程:KeyDown与KeyUp事件使用实例|方法

KeyDown与KeyUp事件

在键盘按下键然后释放的过程中有3个事件发生,分别为KeyDown事件、KeyPress事件和KeyUp事件。KeyDown和KeyUp事件让应用程序捕捉用户在键盘上按下的特殊键或某些特定键甚至组合键,只有在想要取得按下键或特殊键的相关信息时,才用到KeyDown和KeyUp事件。下面对KeyDown和KeyUp事件分别进行介绍。

1.KeyDown事件

KeyDown事件在首次按下某个键时发生。

示例

KeyDown事件的使用

本示例中,判断用户是否按下特殊键,如果是,则显示在窗体的标题栏上。示例运行结果如图1所示。



图1 同时按下Ctrl+Alt+F

private void textBox1_KeyDown(object sender, KeyEventArgs e)

{

string G_str_Mode = "";

string G_str_text = e.KeyCode + ":" + e.Modifiers + ":" + e.KeyData + ":" + "(" + e.KeyValue + ")";

if (e.Shift == True)

G_str_Mode = "Shift 键被按下";

if (e.Control == True)

G_str_Mode = "Ctrl 键被按下";

if (e.Alt == True)

G_str_Mode = "Alt 键被按下";

this.Text = G_str_text + G_str_Mode;

}

完整程序代码如下:

★ ★★★★frmKeyDownUP.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace _2_08

{

public partial class frmKeyDownUP : Form

{

public frmKeyDownUP()

{

InitializeComponent();

}

private void frmKeyDownUP_KeyDown(object sender, KeyEventArgs e)

{

}

private void frmKeyDownUP_Load(object sender, EventArgs e)

{

}

private void textBox1_KeyDown(object sender, KeyEventArgs e)

{

string G_str_Mode = "";

string G_str_text = e.KeyCode + ":" + e.Modifiers + ":" + e.KeyData + ":" + "(" + e.KeyValue + ")";

if (e.Shift == true)

G_str_Mode = "Shift 键被按下";

if (e.Control == true)

G_str_Mode = "Ctrl 键被按下";

if (e.Alt == true)

G_str_Mode = "Alt 键被按下";

this.Text = G_str_text + G_str_Mode;

}

private void frmKeyDownUP_KeyUp(object sender, KeyEventArgs e)

{

}

}

}

★ ★★★★frmKeyDownUP.Designer.cs窗体设计文件完整程序代码★★★★★

namespace _2_08

{

partial class frmKeyDownUP

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.textBox1 = new System.Windows.Forms.TextBox();

this.SuspendLayout();

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(82, 77);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(0, 12);

this.label1.TabIndex = 1;

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(61, 77);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(251, 21);

this.textBox1.TabIndex = 0;

this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);

//

// frmKeyDownUP

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(555, 137);

this.Controls.Add(this.label1);

this.Controls.Add(this.textBox1);

this.Name = "frmKeyDownUP";

this.Text = "frmKeyDownUP";

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.frmKeyDownUP_KeyUp);


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759