将键盘宏命令发送到游戏Windows

我想为游戏做一个宏程序。 但是仅向游戏应用程序(游戏窗口)发送密钥存在问题。 我正在使用keybd_event API将密钥发送到游戏窗口。 但我只想在我的宏程序运行时将密钥发送到游戏窗口,而不是发送到资源管理器或任何打开的窗口。 当我改变窗户时,它仍然发送密钥。 我尝试使用Interaction.AppVisual Basic.dll引用。 但Interaction.App只关注游戏窗口。

我找不到任何关于我的问题。 谁能帮我? 感谢名单

我解决了我的问题。 在这个领域里 ;

PostMessage(hWnd,WM_KEYDOWN,key, {必须给密钥的lParam} );

否则它不起作用。我们可以使用Microsoft的Spy ++工具控制ChildWindow类。

谢谢大家的帮助。

你一直在检索窗口的句柄,还是你还记得它?

如果使用FindWindow()API,则只需存储Handle并使用SendMessage API手动发送键/鼠标事件。

FindWindow API:
http://www.pinvoke.net/default.aspx/user32.FindWindowEx

SendMessage API:
http://www.pinvoke.net/default.aspx/user32/SendMessage.html

VB

 Private Const WM_KEYDOWN As Integer = &H100 Private Const WM_KEYUP As Integer = &H101 

C#

 private static int WM_KEYDOWN = 0x100 private static int WM_KEYUP = 0x101 
 class SendKeySample { private static Int32 WM_KEYDOWN = 0x100; private static Int32 WM_KEYUP = 0x101; [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true)] static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); public static IntPtr FindWindow(string windowName) { foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { if (p.MainWindowHandle != IntPtr.Zero && p.MainWindowTitle.ToLower() == windowName.ToLower()) return p.MainWindowHandle; } return IntPtr.Zero; } public static IntPtr FindWindow(IntPtr parent, string childClassName) { return FindWindowEx(parent, IntPtr.Zero, childClassName, string.Empty); } public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key) { PostMessage(hWnd, WM_KEYDOWN, key, 0); } } 

致电代码

  var hWnd = SendKeySample.FindWindow("Untitled - Notepad"); var editBox = SendKeySample.FindWindow(hWnd, "edit"); SendKeySample.SendKey(editBox, Keys.A); 

如果您想与游戏进行通信,通常需要处理DirectInput,而不是普通的键盘API。