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

时间:2009-01-02 15:09:44

标签: c# api keyboard sendkeys

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

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

5 个答案:

答案 0 :(得分:2)

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

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

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

感谢大家的帮助。

答案 1 :(得分:1)

您是否一直在检索窗口的句柄,或者您是否记得它?

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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);

答案 4 :(得分:0)

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