Csharp/C Sharp/Event/Key Event — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:34, 26 мая 2010
Содержание
- 1 Alt key pressed
- 2 Check KeyCode from KeyEventArgs
- 3 Close Form with Pressing X key
- 4 Control Key pressed
- 5 Displaying information about the key the user pressed
- 6 Displays a key pressed by the user
- 7 Get Async Key State
- 8 Get Key action information
- 9 Keyboard Sample
- 10 Key Press
- 11 Key Timer
- 12 Shift key pressed
Alt key pressed
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState( System.Windows.Forms.Keys vKey);
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label lbl;
private System.Windows.Forms.Button cmdAsyncState;
public Form1() {
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lbl.Text = "Key Down: " + e.KeyValue.ToString();
lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
lbl.Text += "\nKey Data: " + e.KeyData.ToString();
if ((e.Modifiers & Keys.Shift) == Keys.Shift)
{
lbl.Text += "\n" + "Shift was held down.";
}
if ((e.Modifiers & Keys.Control) == Keys.Control)
{
lbl.Text += "\n" + "Control was held down.";
}
if (e.Alt)
{
lbl.Text += "\n" + "Alt was held down.";
}
}
private void cmdAsyncState_Click(object sender, EventArgs e)
{
int state = Convert.ToInt32(GetAsyncKeyState(Keys.A).ToString());
switch (state)
{
case 0:
lbl.Text = "A has not been pressed since the last call.";
break;
case 1:
lbl.Text = "A is not currently pressed, but has been pressed since the last call.";
break;
case -32767:
lbl.Text = "A is currently pressed.";
break;
}
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.lbl = new System.Windows.Forms.Label();
this.cmdAsyncState = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(36, 36);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(205, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "<Text will never appear here>";
//
// lbl
//
this.lbl.AutoSize = true;
this.lbl.Location = new System.Drawing.Point(35, 77);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(0, 0);
this.lbl.TabIndex = 1;
//
// cmdAsyncState
//
this.cmdAsyncState.Location = new System.Drawing.Point(36, 202);
this.cmdAsyncState.Name = "cmdAsyncState";
this.cmdAsyncState.Size = new System.Drawing.Size(141, 24);
this.cmdAsyncState.TabIndex = 2;
this.cmdAsyncState.Text = "GetAsyncState() for \"A\"";
this.cmdAsyncState.Click += new System.EventHandler(this.cmdAsyncState_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdAsyncState);
this.Controls.Add(this.lbl);
this.Controls.Add(this.textBox1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "KeyTest";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Check KeyCode from KeyEventArgs
using System;
using System.Drawing;
using System.Windows.Forms;
class ExitOnX: Form
{
public static void Main()
{
Application.Run(new ExitOnX());
}
public ExitOnX()
{
Text = "Exit on X";
}
protected override void OnKeyDown(KeyEventArgs kea)
{
if (kea.KeyCode == Keys.X)
Close();
}
}
Close Form with Pressing X key
using System;
using System.Drawing;
using System.Windows.Forms;
class ExitOnX: Form
{
public static void Main()
{
Application.Run(new ExitOnX());
}
public ExitOnX()
{
Text = "Exit on X";
}
protected override void OnKeyDown(KeyEventArgs kea)
{
if (kea.KeyCode == Keys.X)
Close();
}
}
Control Key pressed
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState( System.Windows.Forms.Keys vKey);
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label lbl;
private System.Windows.Forms.Button cmdAsyncState;
public Form1() {
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lbl.Text = "Key Down: " + e.KeyValue.ToString();
lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
lbl.Text += "\nKey Data: " + e.KeyData.ToString();
if ((e.Modifiers & Keys.Shift) == Keys.Shift)
{
lbl.Text += "\n" + "Shift was held down.";
}
if ((e.Modifiers & Keys.Control) == Keys.Control)
{
lbl.Text += "\n" + "Control was held down.";
}
if (e.Alt)
{
lbl.Text += "\n" + "Alt was held down.";
}
}
private void cmdAsyncState_Click(object sender, EventArgs e)
{
int state = Convert.ToInt32(GetAsyncKeyState(Keys.A).ToString());
switch (state)
{
case 0:
lbl.Text = "A has not been pressed since the last call.";
break;
case 1:
lbl.Text = "A is not currently pressed, but has been pressed since the last call.";
break;
case -32767:
lbl.Text = "A is currently pressed.";
break;
}
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.lbl = new System.Windows.Forms.Label();
this.cmdAsyncState = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(36, 36);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(205, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "<Text will never appear here>";
//
// lbl
//
this.lbl.AutoSize = true;
this.lbl.Location = new System.Drawing.Point(35, 77);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(0, 0);
this.lbl.TabIndex = 1;
//
// cmdAsyncState
//
this.cmdAsyncState.Location = new System.Drawing.Point(36, 202);
this.cmdAsyncState.Name = "cmdAsyncState";
this.cmdAsyncState.Size = new System.Drawing.Size(141, 24);
this.cmdAsyncState.TabIndex = 2;
this.cmdAsyncState.Text = "GetAsyncState() for \"A\"";
this.cmdAsyncState.Click += new System.EventHandler(this.cmdAsyncState_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdAsyncState);
this.Controls.Add(this.lbl);
this.Controls.Add(this.textBox1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "KeyTest";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Displaying information about the key the user pressed
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
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");
}
}
Displays a key pressed by the user
using System.Drawing;
using System;
using System.Windows.Forms;
public class TryKey : Form {
private char theKey = "d";
public TryKey() {
Size = new Size(300,200);
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawString(theKey.ToString(), new Font("Arial", 36, FontStyle.Bold), Brushes.Red, 100, 50);
base.OnPaint(e);
}
protected override void OnKeyDown(KeyEventArgs e){
if (e.Control){
Console.WriteLine("Control");
}
if (e.KeyCode == Keys.Right){
Console.WriteLine("Right");
}
else if (e.KeyCode == Keys.Left){
Console.WriteLine("Left");
}
Invalidate();
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e) {
Console.WriteLine("Key Up");
base.OnKeyUp(e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
if (char.IsLetterOrDigit(e.KeyChar)){
theKey = e.KeyChar;
}
Invalidate();
base.OnKeyPress(e);
}
public static void Main() {
Application.Run(new TryKey());
}
}
Get Async Key State
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState( System.Windows.Forms.Keys vKey);
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label lbl;
private System.Windows.Forms.Button cmdAsyncState;
public Form1() {
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lbl.Text = "Key Down: " + e.KeyValue.ToString();
lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
lbl.Text += "\nKey Data: " + e.KeyData.ToString();
if ((e.Modifiers & Keys.Shift) == Keys.Shift)
{
lbl.Text += "\n" + "Shift was held down.";
}
if ((e.Modifiers & Keys.Control) == Keys.Control)
{
lbl.Text += "\n" + "Control was held down.";
}
if (e.Alt)
{
lbl.Text += "\n" + "Alt was held down.";
}
}
private void cmdAsyncState_Click(object sender, EventArgs e)
{
int state = Convert.ToInt32(GetAsyncKeyState(Keys.A).ToString());
switch (state)
{
case 0:
lbl.Text = "A has not been pressed since the last call.";
break;
case 1:
lbl.Text = "A is not currently pressed, but has been pressed since the last call.";
break;
case -32767:
lbl.Text = "A is currently pressed.";
break;
}
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.lbl = new System.Windows.Forms.Label();
this.cmdAsyncState = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(36, 36);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(205, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "<Text will never appear here>";
//
// lbl
//
this.lbl.AutoSize = true;
this.lbl.Location = new System.Drawing.Point(35, 77);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(0, 0);
this.lbl.TabIndex = 1;
//
// cmdAsyncState
//
this.cmdAsyncState.Location = new System.Drawing.Point(36, 202);
this.cmdAsyncState.Name = "cmdAsyncState";
this.cmdAsyncState.Size = new System.Drawing.Size(141, 24);
this.cmdAsyncState.TabIndex = 2;
this.cmdAsyncState.Text = "GetAsyncState() for \"A\"";
this.cmdAsyncState.Click += new System.EventHandler(this.cmdAsyncState_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdAsyncState);
this.Controls.Add(this.lbl);
this.Controls.Add(this.textBox1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "KeyTest";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Get Key action information
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.GroupBox GroupBox1;
private System.Windows.Forms.Label Label4;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.PictureBox pic;
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.Button cmd;
private System.Windows.Forms.Label Label2;
private System.Windows.Forms.Label Label3;
private System.Windows.Forms.ListBox eventLogList;
public Form1() {
InitializeComponent();
}
private void Log(String data)
{
eventLogList.Items.Add(data);
int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
}
private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
Log("Key Press: " + e.KeyChar.ToString());
}
private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
}
private void txt_TextChanged(object sender, System.EventArgs e)
{
Log("Changed: " + " Text is: " + txt.Text);
}
private void pic_MouseEnter(object sender, System.EventArgs e)
{
Log("Mouse Enter");
}
private void pic_MouseHover(object sender, System.EventArgs e)
{
Log("Mouse Hover");
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_Click(object sender, System.EventArgs e)
{
Log("Click");
}
private void pic_DoubleClick(object sender, System.EventArgs e)
{
Log("Double Click");
}
private void pic_MouseLeave(object sender, System.EventArgs e)
{
Log("Mouse Leave");
}
private void InitializeComponent()
{
this.GroupBox1 = new System.Windows.Forms.GroupBox();
this.Label4 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.pic = new System.Windows.Forms.PictureBox();
this.txt = new System.Windows.Forms.TextBox();
this.cmd = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.eventLogList = new System.Windows.Forms.ListBox();
this.GroupBox1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit();
this.SuspendLayout();
//
// GroupBox1
//
this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.GroupBox1.Controls.Add(this.Label4);
this.GroupBox1.Controls.Add(this.Label1);
this.GroupBox1.Controls.Add(this.pic);
this.GroupBox1.Controls.Add(this.txt);
this.GroupBox1.Controls.Add(this.cmd);
this.GroupBox1.Controls.Add(this.Label2);
this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.GroupBox1.Location = new System.Drawing.Point(7, 0);
this.GroupBox1.Name = "GroupBox1";
this.GroupBox1.Size = new System.Drawing.Size(384, 148);
this.GroupBox1.TabIndex = 12;
this.GroupBox1.TabStop = false;
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(92, 108);
this.Label4.Name = "Label4";
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.TabIndex = 5;
this.Label4.Text = "And here:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(6, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(144, 16);
this.Label1.TabIndex = 2;
this.Label1.Text = "Test keyboard events here:";
//
// pic
//
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(156, 48);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(192, 48);
this.pic.TabIndex = 3;
this.pic.TabStop = false;
this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
this.pic.Click += new System.EventHandler(this.pic_Click);
this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// txt
//
this.txt.Location = new System.Drawing.Point(156, 20);
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(192, 21);
this.txt.TabIndex = 1;
this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// cmd
//
this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmd.Location = new System.Drawing.Point(156, 100);
this.cmd.Name = "cmd";
this.cmd.Size = new System.Drawing.Size(88, 28);
this.cmd.TabIndex = 4;
this.cmd.Text = "Button1";
this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.cmd.Click += new System.EventHandler(this.pic_Click);
this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(20, 52);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 2;
this.Label2.Text = "Test mouse events here:";
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(23, 100);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(64, 24);
this.Label3.TabIndex = 11;
this.Label3.Text = "Label3";
//
// eventLogList
//
this.eventLogList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.eventLogList.FormattingEnabled = true;
this.eventLogList.IntegralHeight = false;
this.eventLogList.Location = new System.Drawing.Point(7, 156);
this.eventLogList.Name = "eventLogList";
this.eventLogList.Size = new System.Drawing.Size(384, 212);
this.eventLogList.TabIndex = 10;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(399, 374);
this.Controls.Add(this.GroupBox1);
this.Controls.Add(this.Label3);
this.Controls.Add(this.eventLogList);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.Text = "Event Tracker";
this.GroupBox1.ResumeLayout(false);
this.GroupBox1.PerformLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Keyboard Sample
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace KeyboardSample
{
/// <summary>
/// Summary description for KeyboardSample.
/// </summary>
public class KeyboardSample : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public KeyboardSample()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(16, 24);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 20);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.textBox1});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Key Monitor";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 20);
this.label1.TabIndex = 6;
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.label2});
this.groupBox2.Location = new System.Drawing.Point(8, 120);
this.groupBox2.Name = "groupBox2";
this.groupBox2.TabIndex = 8;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Keys Enumeration";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(168, 20);
this.label2.TabIndex = 9;
//
// KeyboardSample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(216, 229);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox2,
this.groupBox1});
this.Name = "KeyboardSample";
this.Text = "KeyboardSample";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new KeyboardSample());
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
label1.Text = Convert.ToString(e.KeyValue);
}
private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
StringBuilder sb = new StringBuilder();
if(e.Shift) sb.Append("Shift, ");
if(e.Alt) sb.Append("Alt, ");
if(e.Control) sb.Append("Ctrl, ");
if(e.KeyCode==Keys.W||e.KeyCode==Keys.R||e.KeyCode==Keys.O||e.KeyCode==Keys.X)
{
sb.Append("Wrox Press!!");
}
else if(e.KeyCode==Keys.Escape&&e.Modifiers==(Keys.Shift|Keys.Alt))
{
sb.Append("Escape - that won"t work!");
}
else if(e.KeyCode == Keys.C && e.Modifiers==(Keys.Alt | Keys.Control))
{
sb.Append("CopyRight");
textBox2.SelectedText = "CopyRight";
textBox2.SelectionLength = 0;
}
else
{
sb.Append(Convert.ToString(e.KeyData));
}
label2.Text = sb.ToString();
}
}
}
Key Press
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace KeyPress
{
/// <summary>
/// Summary description for KeyPress.
/// </summary>
public class KeyPress : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public KeyPress()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(176, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 32);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// KeyPress
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1});
this.Name = "KeyPress";
this.Text = "KeyPress";
this.Load += new System.EventHandler(this.KeyPress_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new KeyPress());
}
private void KeyPress_Load(object sender, System.EventArgs e)
{
this.KeyPreview=true;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
MessageBox.Show("Key Pressed = " + e.KeyChar.ToString());
}
}
}
Key Timer
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace Wrox.ProgrammingWindowsGUI.Chapter5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class KeyTimer : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
private uint start = 0;
private uint stop = 0;
[DllImport("kernel32.dll")]
public static extern uint GetTickCount();
public KeyTimer()
{
//
// Required for Windows Form Designer support
//
//InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 142);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new KeyTimer());
}
protected override void OnKeyDown(KeyEventArgs args)
{
start = GetTickCount();
}
protected override void OnKeyUp(KeyEventArgs args)
{
stop = GetTickCount();
uint elapsed = (stop - start);
MessageBox.Show(Convert.ToString(args.KeyData)+", time elapsed: "+Convert.ToString(elapsed)+" msecs");
}
}
}
Shift key pressed
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState( System.Windows.Forms.Keys vKey);
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label lbl;
private System.Windows.Forms.Button cmdAsyncState;
public Form1() {
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lbl.Text = "Key Down: " + e.KeyValue.ToString();
lbl.Text += "\nKey Code: " + e.KeyCode.ToString();
lbl.Text += "\nKey Data: " + e.KeyData.ToString();
if ((e.Modifiers & Keys.Shift) == Keys.Shift)
{
lbl.Text += "\n" + "Shift was held down.";
}
if ((e.Modifiers & Keys.Control) == Keys.Control)
{
lbl.Text += "\n" + "Control was held down.";
}
if (e.Alt)
{
lbl.Text += "\n" + "Alt was held down.";
}
}
private void cmdAsyncState_Click(object sender, EventArgs e)
{
int state = Convert.ToInt32(GetAsyncKeyState(Keys.A).ToString());
switch (state)
{
case 0:
lbl.Text = "A has not been pressed since the last call.";
break;
case 1:
lbl.Text = "A is not currently pressed, but has been pressed since the last call.";
break;
case -32767:
lbl.Text = "A is currently pressed.";
break;
}
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.lbl = new System.Windows.Forms.Label();
this.cmdAsyncState = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(36, 36);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(205, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "<Text will never appear here>";
//
// lbl
//
this.lbl.AutoSize = true;
this.lbl.Location = new System.Drawing.Point(35, 77);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(0, 0);
this.lbl.TabIndex = 1;
//
// cmdAsyncState
//
this.cmdAsyncState.Location = new System.Drawing.Point(36, 202);
this.cmdAsyncState.Name = "cmdAsyncState";
this.cmdAsyncState.Size = new System.Drawing.Size(141, 24);
this.cmdAsyncState.TabIndex = 2;
this.cmdAsyncState.Text = "GetAsyncState() for \"A\"";
this.cmdAsyncState.Click += new System.EventHandler(this.cmdAsyncState_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdAsyncState);
this.Controls.Add(this.lbl);
this.Controls.Add(this.textBox1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "KeyTest";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}