anaval
anaval

Reputation: 1138

Custom Hotkey for a game

I made a program that lets me create hotkeys for games, such as warcraft.exe and dota.exe.

My application name is "Hotkey.exe".

On KeyDown, the alt key should instead simulate a press of the A key in dota.exe. On KeyUp, the alt key should instead simulate a press of the B key in dota.exe.

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
            }
        }


    }
}

What can I add to make this work?

Upvotes: 2

Views: 914

Answers (2)

Amin
Amin

Reputation: 1013

You can use Windows API RegisretHotKey like this:

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

}

See also:
RegisterHotKey function
About Hot Key controls

Upvotes: 3

Tathagat Verma
Tathagat Verma

Reputation: 548

Here is one suggested approach:

  • Write functions to the actual logic
  • Implement Key-Press/Down and Key-Up events for the win-form
  • Call the respective functions on he events by checking the "which key is pressed" logic on respective events

This is all that I could suggest from what I understood, of the little info that you've provided.

Hope this at least gives you the right direction to work upon.

Upvotes: 0

Related Questions