KeyEventArg.cs
using System;
namespace EventTest
{
internal class KeyEventArg : EventArgs
{
private char keyChar;
public KeyEventArg(char k)
: base()
{
this.keyChar = k;
}
public char KeyChar
{
get
{
return keyChar;
}
}
}
internal class KeyInput
{
public delegate void KeyDonwHandle(object sender, KeyEventArg e);
public event KeyDonwHandle KeyDown;
public void run()
{
bool finish = false;
do
{
Console.WriteLine("请输入:");
string s = Console.ReadLine();
char c = (s == "") ? ' ' : char.ToUpper(s[0]);
switch (c)
{
case 'X':
finish = true;
break;
default:
KeyEventArg key = new KeyEventArg(c);
KeyDown(this, key);
break;
}
} while (!finish);
}
}
internal class ReceiveEvent
{
public ReceiveEvent(KeyInput moin)
{
moin.KeyDown += new KeyInput.KeyDonwHandle(moin_KeyDown);
}
void moin_KeyDown(object sender, KeyEventArg e)
{
Console.WriteLine("事件被除法");
Console.WriteLine(e.KeyChar);
}
}
}
实例化:
using System;
namespace EventTest
{
class Program
{
static void Main(string[] args)
{
KeyInput k = new KeyInput();
ReceiveEvent r = new ReceiveEvent(k);
k.run();
}
}
}