pha*_*unk 2 c# keyboard winapi keyboard-hook
如何在不是我的C#应用程序的窗口中模拟键击?
现在我正在使用SendKeys.Send()但它不起作用.问题是我有一个全局键盘钩子,所以我直接从键盘捕获输入,SendKeys.Send()并不像真正的键盘笔划.
最好的方法是以这种方式模拟真正的击键,无论我在哪个应用程序,我的程序都会抓住它,好像有人按下了一个键.
我想我发现了部分问题.如果按下某个键,则调用此事件:
static void KeyBoardHook_KeyPressed(object sender, KeyPressedEventArgs e)
{
// Writes the pressed key in the console (it works)
Console.WriteLine(e.KeyCode.ToString());
// Check if pressed key is Up Arrow (it works and enters the condition)
if(e.KeyCode == Keys.Up)
{
// Send the key again. (does not work)
SendKeys.Send("{UP}");
}
}
Run Code Online (Sandbox Code Playgroud)
我这样试过:
static void KeyBoardHook_KeyPressed(object sender, KeyPressedEventArgs e)
{
// Writes the pressed key in the console (it works)
Console.WriteLine(e.KeyCode.ToString());
// Check if pressed key is Up Arrow (it works and enters the condition)
if(e.KeyCode == Keys.Up)
{
// Send the key again. (does not work)
PostMessage(proc.MainWindowHandle,WM_KEYDOWN, VK_UP,0);
}
}
Run Code Online (Sandbox Code Playgroud)
但它也不起作用.事情是因为我在我的事件中发送密钥,它是否会调用自己,因为按下了一个键?万一有人需要它,上面的代码.
[STAThread]
static void Main(string args)
{
KeyBoardHook.CreateHook();
KeyBoardHook.KeyPressed += KeyBoardHook_KeyPressed;
Application.Run();
KeyBoardHook.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
如果你需要KeyBoardHook上课,我也可以发布.
我的猜测是我的键盘钩子正在捕捉低级键盘输出,而这SendKeys只是模拟击键,所以我的钩子没有抓住它.有人想到一个解决方案吗?
我建议你使用这个非常酷的库来掩盖你的所有复杂性,这里有Windows输入模拟器:http://inputsimulator.codeplex.com/
我相信它基于Windows的SendInput功能.
| 归档时间: |
|
| 查看次数: |
10165 次 |
| 最近记录: |