【问题标题】:Sending Keyboard Macro Commands to Game Windows将键盘宏命令发送到游戏窗口
【发布时间】:2010-09-29 06:03:30
【问题描述】:

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

我找不到任何关于我的问题的信息。谁能帮我?谢谢

【问题讨论】:

    标签: c# api keyboard sendkeys


    【解决方案1】:

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

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

    否则不起作用。我们可以用微软的Spy++工具控制ChildWindow Class。

    感谢大家的帮助。

    【讨论】:

      【解决方案2】:

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

      如果您使用 FindWindow() API,您可以简单地存储 Handle 并使用 SendMessage API 手动发送键/鼠标事件。

      【讨论】:

        【解决方案3】:

        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
        

        【讨论】:

          【解决方案4】:
          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);
          

          【讨论】:

            【解决方案5】:

            如果您想与游戏通信,您通常必须处理 DirectInput,而不是普通的键盘 API。

            【讨论】:

            • 完全同意这一点,但他似乎已经可以使用键盘事件api,并且只有在窗口失去焦点时才会出现问题。
            • 不一定 - 普通键盘 API 的级别低于 DirectInput,虽然使用 DirecctInput 可能更简单,但两种方式都应该工作。
            猜你喜欢
            • 2012-01-22
            • 2012-01-26
            • 1970-01-01
            • 2019-03-21
            • 2012-03-03
            • 1970-01-01
            • 1970-01-01
            • 2014-08-09
            • 1970-01-01
            相关资源
            最近更新 更多