在C#中通过钩子重定向keydown?

发布于 2024-11-18 09:52:56 字数 1162 浏览 2 评论 0原文

我想编写一个像游戏热键助手这样的应用程序,例如,在魔兽争霸3中,我出于某种原因使用小键盘键。是否可以通过按“X”键向操作系统发送“Numpad 1 key pressed”消息?我一直在尝试使用钩子来做到这一点,但我无法弄清楚。我希望我说清楚了。

我已将此类用于挂钩:.NET 中的全局系统挂钩

简单来说我的代码是这样的:

globalKeyboardHook hook = new globalKeyboardHook(); // A Global Hook

private void btnHook_Click(object sender, EventArgs e)
{
    hook.hook(); // Set the hook
    hook.HookedKeys.Add(Keys.X); // Hook the X key
    // i removed other hooks purify the code, six hooks total.
    hook.KeyDown += new KeyEventHandler(hook_KeyDown); // add eventhandler
    Program.hookActive = true; // inform the program that hook is active.
    Guncelle(); // update form components (buttons enabling/disabling according to hook status etc.)
}

private void hook_KeyDown(object sender, KeyEventArgs e)
{
    KeyEventArgs k = new KeyEventArgs(Keys.NoName);
    switch (e.KeyCode)
    {
        case Keys.X:
            k = new KeyEventArgs(Keys.NumPad1);
            OnKeyDown(k);
            break;
            // i removed other cases to purify the code, six cases total.
    }
}

I want to write an application like hotkey helpers for games, for example, in Warcraft 3, I use numpad keys for some reason. Is it possible to send a "Numpad 1 key pressed" message to the OS by pressing the "X" key? I've been trying to do this using hooks but I couldn't figure out. I hope I was clear.

I've used this class for hooks: Global System Hooks in .NET.

Simply my code is like this:

globalKeyboardHook hook = new globalKeyboardHook(); // A Global Hook

private void btnHook_Click(object sender, EventArgs e)
{
    hook.hook(); // Set the hook
    hook.HookedKeys.Add(Keys.X); // Hook the X key
    // i removed other hooks purify the code, six hooks total.
    hook.KeyDown += new KeyEventHandler(hook_KeyDown); // add eventhandler
    Program.hookActive = true; // inform the program that hook is active.
    Guncelle(); // update form components (buttons enabling/disabling according to hook status etc.)
}

private void hook_KeyDown(object sender, KeyEventArgs e)
{
    KeyEventArgs k = new KeyEventArgs(Keys.NoName);
    switch (e.KeyCode)
    {
        case Keys.X:
            k = new KeyEventArgs(Keys.NumPad1);
            OnKeyDown(k);
            break;
            // i removed other cases to purify the code, six cases total.
    }
}

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

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

发布评论

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

评论(1

榆西 2024-11-25 09:52:56

是的,您可以使用 WinAPI SendInput()keyb_event() 调用。例如:

keybd_event(VK_NUMPAD1, 0, 0, 0)

Yes, you can use the WinAPI SendInput() or keyb_event() calls. For example:

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