【发布时间】:2016-02-11 04:54:08
【问题描述】:
如何在 .NET 中模拟按钮按下?
我可以使用 AutoHotKey 轻松做到这一点:
#Persistent
SetTimer,PressTheKey,1000
Return
PressTheKey:
Send, {F5}
Return
任何应用程序都会知道该按钮被按下(就像我按下它一样)。我试着做同样的事情:
[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
或:
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
或使用 Windows 输入模拟器 (https://inputsimulator.codeplex.com/):
inputSimulator.Keyboard.KeyDown(VirtualKeyCode.VK_H)
它们都在记事本或其他一些应用程序中工作,但它们从未在我的全屏游戏中工作。知道如何实现吗?我想要一个在系统级别上模拟全局点击的解决方案。
【问题讨论】:
-
你看过这个帖子吗:stackoverflow.com/questions/10787949/… ?
-
你看过这个。 inputsimulator.codeplex.com
-
回答上面的评论。是的,我试过 SendKeys.SendWait() (忘了提)。我也尝试过 InputSimulator(提到过)——它们对我没有用。他们似乎将击键发送到特定的应用程序,而应用程序可以忽略它......我需要一些模拟键盘击键的东西......