自定义游戏热键

本文关键字:游戏 自定义 | 更新日期: 2023-09-27 18:27:59

我制作了一个程序,可以为游戏创建热键,例如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函数
关于热键控制

这里有一个建议的方法:

  • 将函数写入实际逻辑
  • 为获胜表格实施按键按下/按下和按键向上活动
  • 通过检查各个事件上的"按下哪个键"逻辑,调用事件上的各个函数

这就是我所能从我所了解的,你所提供的一些小信息中提出的全部建议。

希望这至少能给你一个正确的工作方向。