如何处理控制台应用程序键preSS事件 [英] how to handle key press event in console application
本文介绍了如何处理控制台应用程序键preSS事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个控制台应用程序,将显示一个pssed控制台屏幕上$ P $关键,我做了这个code迄今:
I want to create a console application that will display the key that is pressed on console screen, I made this code so far:
static void Main(string[] args)
{
// this is absolutely wrong, but I hope you get what I mean
PreviewKeyDownEventArgs += new PreviewKeyDownEventArgs(keylogger);
}
private void keylogger(KeyEventArgs e)
{
Console.Write(e.KeyCode);
}
我想知道,我应该在主要类型,以便我可以调用该事件?
I want to know, what should I type in main so I can call that event?
推荐答案
有关控制台应用程序,你可以做到这一点,做的,而
循环运行,直到你preSS X
For console application you can do this, the do while
loop runs untill you press x
public class Program
{
public static void Main()
{
ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
Console.WriteLine(keyinfo.Key + " was pressed");
}
while (keyinfo.Key != ConsoleKey.X);
}
}
这如果你的控制台应用程序具有焦点只会工作。如果您要收集全系统的关键preSS事件,你可以使用 windows钩子
This will only work if your console application has focus. If you want to gather system wide key press events you can use windows hooks
这篇关于如何处理控制台应用程序键preSS事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文