简体   繁体   中英

configurable hotkeys for button presses c#

Im looking for a way that so Users that use my Program can change the Hotkeys theirself so its not bound to D,F12,K,F,A and so on.. i kinda want that ppl can change it via a Textbox or maybe via a Settings File. Been Stuck on it for couple Weeks now and i just cant find a way to make it happen, its my first Program im working on.

  public void gHook_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.D: // stuff Salvagebtn.PerformClick(); break; case Keys.F12: // stuff pausebtn.PerformClick(); break; case Keys.K: //stuff Geardropbtn.PerformClick(); break; case Keys.F: //stuff Gamblebtn.PerformClick(); break; case Keys.A: //stuff LeftClickSpambtn.PerformClick(); break; case Keys.H: // stuff openGRbtn.PerformClick(); break; case Keys.B: //stuff gemupbtn.PerformClick(); break; } } 

I had a few minutes, during commercials. So, I wrote the following:

EDIT : Code wrapped with a global keyboard handler. You will also need to add a reference to PresentationCore and WindowsBase ...

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Input;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        [DllImport("user32.dll")]
        static extern int MapVirtualKey(uint uCode, uint uMapType);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100,
                          WM_KEYUP = 0x0101,
                          S_WM_KEYDOWN = 0x0104,
                          S_WM_KEYUP = 0x0105;

        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;

        private static Form1 hookForm;

        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        static bool bShortcutPressed;

        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (hookForm.Handle != GetForegroundWindow())
            {
                if (nCode >= 0 && ((wParam == (IntPtr)WM_KEYDOWN) || (wParam == (IntPtr)S_WM_KEYDOWN)))
                {
                    int vkCode = Marshal.ReadInt32(lParam);

                    bool bCtrl = (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
                    bool bAlt = (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
                    bool bShift = (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift));

                    Keys hookKey = (Keys)vkCode;

                    hookKey = (bCtrl) ? ((Keys.Control | hookKey)) : hookKey;
                    hookKey = (bAlt) ? ((Keys.Alt | hookKey)) : hookKey;
                    hookKey = (bShift) ? ((Keys.Shift | hookKey)) : hookKey;

                    Debug.Print($"hookKey {hookKey} {bCtrl} {bAlt} {bShift}");

                    if (!bShortcutPressed && dicTest.ContainsValue(hookKey))
                    {
                        hookForm.OnKeyDown(new System.Windows.Forms.KeyEventArgs(hookKey));
                        bShortcutPressed = true; Debug.Print($"{bShortcutPressed}");
                    }
                }

                if (nCode >= 0 && (((wParam == (IntPtr)WM_KEYUP) || (wParam == (IntPtr)S_WM_KEYUP)))) { bShortcutPressed = false; Debug.Print($"{bShortcutPressed}"); }
            }

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        static Dictionary<string, Keys> dicTest = new Dictionary<string, Keys>();

        public void AddHotKey(Action function, Keys key, bool ctrl = false, bool shift = false, bool alt = false)
        {
            KeyDown += delegate (object sender, System.Windows.Forms.KeyEventArgs e) { if (IsHotkey(e, key, ctrl, shift, alt)) { function(); } };
        }

        public bool IsHotkey(System.Windows.Forms.KeyEventArgs eventData, Keys key, bool ctrl = false, bool shift = false, bool alt = false) =>
                             eventData.KeyCode == key && eventData.Control == ctrl && eventData.Shift == shift && eventData.Alt == alt;

        public Form1() => InitializeComponent();

        private void Form1_Load(object sender, EventArgs e)
        {
            _hookID = SetHook(_proc);

            hookForm = this;

            KeyPreview = true;

            String[] names = Enum.GetNames(typeof(Keys));

            foreach (string key in names) { comboBox1.Items.Add(key); }

            comboBox1.SelectedItem = comboBox1.Items[0];

            KeyDown += Form1_KeyDown;
        }

        private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            Debug.Print($"Form1_KeyDown : {e.KeyData}");
        }

        protected override void OnFormClosing(FormClosingEventArgs e) => UnhookWindowsHookEx(_hookID);

        private void button1_Click(object sender, EventArgs e)
        {
            Keys hookKey = (Keys)Enum.Parse(typeof(Keys), comboBox1.Text);

            hookKey = (checkBox1.Checked) ? ((Keys.Control | hookKey)) : hookKey;
            hookKey = (checkBox2.Checked) ? ((Keys.Alt | hookKey)) : hookKey;
            hookKey = (checkBox3.Checked) ? ((Keys.Shift | hookKey)) : hookKey;

            if (!dicTest.ContainsValue(hookKey))
            {
                Debug.Print($"Going to add : {hookKey} : to our Shortcut Dictionary<>");

                dicTest.Add("test", hookKey);

                AddHotKey(() => { button2.PerformClick(); }, (Keys)new KeysConverter().ConvertFrom(comboBox1.Text), checkBox1.Checked, checkBox3.Checked, checkBox2.Checked);

                //checkBox1.Enabled = checkBox2.Enabled = checkBox3.Enabled = comboBox1.Enabled = button1.Enabled = false;

                label2.Text = "Go ahead and tryout your Shortcut now ...";
            }
            else { label2.Text = "Shortcut key is already stored in our Dictionary<> ..."; }
        }

        private void button2_Click(object sender, EventArgs e) { Debug.Print($"Button2 was clicked"); }
    }
}

You're presented with:

在此处输入图片说明

Trying the desired shortcut out:

在此处输入图片说明

Just an example, to show there are many ways to do things.

Easy to write them out to a file or something. If you were going to ... I'd change the AddHotKey Action param to a string which contains a function name and then using different methods you can have it invoke that function.

Really though, I'd incorporate a Dictionary as the one talked about below.

There's a myriad of different ways you could handle this. Personally, I'd opt for using a Dictionary with the Keycode as the Key, and a delegate/action as the value. Then on the keypress, you just call the function in the dictionary that's associated with that keycode. Something along the lines of:

Dictionary<KeyCode, System.Action> keyDict = new Dictionary<KeyCode, System.Action>();

// populate it however you wish. Whether it's user entry or from a file or something.
keyDict.Add(KeyCode.D, SomeFunction);

If you want to change the delegate in the dictionary:

keyDict[KeyCode.D] = SomeOtherFunction;

Then later in the event handler:

public void gHook_KeyDown(object sender, KeyEventArgs e)
{
    keyDict[e.KeyCode].Invoke();
}

public void SomeFunction()
{
    Console.WriteLine("called some function");
}

(This question might have a bit more detail if needed: Create dictionary with dynamic keys in C# )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM