将键盘宏命令发送到游戏窗口

发布于 2024-07-11 20:14:38 字数 307 浏览 11 评论 0原文

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

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

I wanna do a macro program for a game. But there is a problem with sending keys to only game application (game window). I am using keybd_event API for sending keys to game window. But I only want to send keys to the game window, not to explorer or any opened window while my macro program is running. When I changed windows its still sending keys. I tried to use Interaction.App with Visual Basic.dll reference. But Interaction.App only Focus the game window.

I couldn't find anything about my problem. Can anyone help me? Thanx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时常饿 2024-07-18 20:14:38

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

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

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

感谢大家的帮助。

i fixed my problem.
in this field ;

PostMessage(hWnd, WM_KEYDOWN, key, {have to give lParam of the key});

otherwise it does not work.And we can control of ChildWindow Class with Spy++ tool of Microsoft.

Thanks everyone for helping.

左秋 2024-07-18 20:14:38

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

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

Are you retrieving the handle of the window all the time, or are you remembering it?

If you use the FindWindow() API, you can simply store the Handle and use the SendMessage API to send key/mouse events manually.

微凉 2024-07-18 20:14:38

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

发送消息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

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
北渚 2024-07-18 20:14:38
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);
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);

    }
}

Calling Code

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

        SendKeySample.SendKey(editBox, Keys.A);
梦在深巷 2024-07-18 20:14:38

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

If you want to communicate with a game, you typically will have to deal with DirectInput, not the normal keyboard API's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文