【问题标题】:Sending keys to a game向游戏发送密钥
【发布时间】:2017-03-30 19:19:55
【问题描述】:

所以我遇到了一个问题,我正在尝试向游戏发送密钥,我在 SetForegroundWindow 的帮助下将游戏置于前台,并且我正在使用 SendInputs API 将密钥发送到游戏。

如果我专注于另一个应用程序,则密钥将发送到该应用程序,但只要我专注于我希望将密钥发送到的应用程序,它们就不会出现在那里。

我正在努力节省一些时间来为我的公会招募公会成员,并且我正在尝试发送游戏密钥。

 [DllImport("user32.dll")]
 private static extern bool SetForegroundWindow(IntPtr hWnd);

 [DllImport("user32.dll")]
 static extern IntPtr GetMessageExtraInfo();

 [DllImport("user32.dll", SetLastError = true)]
 static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

 Process[] procs = Process.GetProcessesByName("BlackDesert64");
 if (procs.Length > 0)
 {
   if (procs[0].MainWindowHandle != IntPtr.Zero)
   {
      SetForegroundWindow(procs[0].MainWindowHandle);
      Thread.Sleep(1000);
   }
 }
 INPUT[] inputs = new INPUT[]
 {
     new INPUT
     {
         type = INPUT_KEYBOARD,
         u = new InputUnion
         {
             ki = new KEYBDINPUT
             {
                 wVk = 0x49,
                 wScan = 0049,
                 dwFlags = KEYEVENTF_UNICODE,
                 dwExtraInfo = GetMessageExtraInfo(),
             }
         }
     },
     new INPUT
     {
         type = INPUT_KEYBOARD,
         u = new InputUnion
         {
             ki = new KEYBDINPUT
             {
                 wVk = 0x49,
                 wScan = 0049,
                 dwFlags = KEYEVENTF_KEYUP,
                 dwExtraInfo = GetMessageExtraInfo(),
             }
         }
    }
};

SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));

其余代码: https://pastebin.com/RUm7A311

更新

所以我找到了允许将密钥发送到使用 DirectX 的游戏的 API 拦截器,我已经设置了它但仍然没有结果.. 谁能指出我正确的方向?

【问题讨论】:

标签: c# .net sendkeys


【解决方案1】:

SendInput 值返回什么?

如果它返回 0,则表明发生了一些错误。您可以尝试调用GetLastError,查看输入是否被UIPI 阻止,或者尝试以本地管理员权限运行您的代码。

您确定 procs[0].MainWindowHandle 是正确的窗口句柄吗?

最后尝试使用SendMessage将消息直接发送到句柄。

【讨论】:

    【解决方案2】:

    使用SendMessage 实现(无需关注窗口)。

    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    [DllImport("User32.dll")]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindows);
    
    [DllImport("User32.dll")]
    private static extern Int32 SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);
    
    void SendKeys()
    {
        IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad");
        if (!hWnd.Equals(IntPtr.Zero))
        {
            IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
            if (!edithWnd.Equals(IntPtr.Zero))
            {
                SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("Test"));
            }
        }
    }
    

    参考:how-do-i-input-to-another-application

    【讨论】:

      【解决方案3】:

      对于您的问题,这里是技巧,

      使用

      String Keys = "Test";
      SendKeys.Send(Keys);
      

      此代码用于将密钥发送到任何应用程序。

      只需将此代码放入 timer_start() 在定时器启动前添加一些延迟,并在执行后停止定时器。

      现在运行你的项目,它会启动计时器,在超时之前打开你的游戏并等待按键!!

      检查此链接,其中包含所有要发送的密钥及其代码 https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx

      【讨论】:

        猜你喜欢
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多