c# - 游戏的自定义热键

标签 c# winforms

我制作了一个程序,可以让我为游戏创建热键,例如 warcraft.exedota.exe

我的应用程序名称是“Hotkey.exe”。

在 KeyDown 上,alt 键应该模拟按 dota.exe 中的 A 键。 在 KeyUp 上,alt 键应该模拟按 dota.exe 中的 B 键。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DN_Hotkey;
using gma.System.Windows;

namespace DN_Hotkey
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        UserActivityHook actHook;
        Keys realkey, altkey;
        private void Form1_Load(object sender, EventArgs e)
        {
            actHook = new UserActivityHook();
            actHook.KeyDown += new KeyEventHandler(MyKeyDown);
            actHook.KeyUp += new KeyEventHandler(MyKeyUp);


            realkey = Keys.Oemtilde;
            altkey = Keys.Alt;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void MyKeyDown(object sender, KeyEventArgs e) 
        {
            if (e.KeyData == Keys.Alt)
            { 
                //sendkey to dota
            }
        }
        private void MyKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Alt)
            {
                //sendkey to dota
            }
        }


    }
}

我可以添加什么来完成这项工作?

最佳答案

您可以像这样使用 Windows API RegisretHotKey:

public partial class Form1 : Form
{
    public const int WM_HOTKEY = 0x0312;
    public const int MOD_NOREPEAT = 0x4000;

    [DllImport("user32")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        RegisterHotKey(this.Handle, 1, MOD_NOREPEAT, 0x76); 
        // Here 0x76 means F7 
        RegisterHotKey(this.Handle, 2, MOD_NOREPEAT, 0x77);
    }

    protected override void WndProc(ref Message m)
    {
        if(m.Msg == WM_HOTKEY)
            switch (m.WParam.ToInt32())
            {
                case 1:
                    // Function that you want to send data to dota
                    break;
                case 2:
                    // Function that you want to send data to dota
                    break;
            }
        base.WndProc(ref m);
    }

}

另请参阅:
RegisterHotKey function
About Hot Key controls

关于c# - 游戏的自定义热键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34447694/

相关文章:

c# - 继承 List<T> 来实现集合是个坏主意吗?

c# - Rebus 停止从 RabbitMQ 检索消息

c# - 较大的显示器会降低 winforms 应用程序的速度吗?

.net - 使用多线程时出现多个通知图标

.net - 使用 Form.Controls 修改控件

c# - chromium - 在初始页面加载时发送自定义 header 信息 c#

c# - MvvmLight,Messenger和Async方法调用

c# - 从 UWP 手机应用更新现有的 Microsoft Band Tile

c# - 从 List<String> 中删除重复的元素

c# - 如何在ajax回发后从代码隐藏调用javascript函数