[C#]Global hotkey

Results 1 to 1 of 1
  1. #1
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    [C#]Global hotkey

    I was looking around the net trying to find a simple keyboard hook. I came across this page: Get back to my program with keyboard

    used it and it was good but I had to keep adding in new ids for each hotkey. I felt it could be improved and heres my improvements:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    
    namespace Test
    {
        enum Constants
        {
            NOMOD = 0x00,
            ALT = 0x01,
            CTRL = 0x02,
            SHIFT = 0x04,
            WIN = 0x08,
            WM_HOTKEY = 0x312
        }
    
    
        class HotKeys
        {
            [DllImport("user32.dll")]
            private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
            [DllImport("user32.dll")]
            private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    
    
            private Dictionary<int, int> _dic = new Dictionary<int, int>();
    
    
            public void addHotKey(Constants mod, Keys key)
            {
                _dic.Add((int)key, (int)mod);
            }
    
    
            public void registerKeys(Form form)
            {
                foreach (KeyValuePair<int, int> pair in _dic)
                    RegisterHotKey(form.Handle, pair.Key, pair.Value, pair.Key);
            }
    
    
            public void disposeKeys(Form form)
            {
                foreach (KeyValuePair<int, int> pair in _dic)
                    UnregisterHotKey(form.Handle, pair.Key);
            }
        }
    }
    to use add as many hotkeys as you want with:
    Code:
    HotKeys _key = new HotKeys();
    _key.addHotKey(Constants.NOMOD, Keys.F1);
    _key.addHotKey(Constants.NOMOD, Keys.F2);
    _key.addHotKey(Constants.NOMOD, Keys.F3);
    to register the keys add into your form load function:
    Code:
    _key.registerKeys(this);
    to unregister the keys add this into your formClosing function:
    Code:
    _key.disposeKeys(this);
    and final to get the hotkeys to actually do something add this function:
    Code:
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)Constants.WM_HOTKEY)
        {
            switch (m.WParam.ToInt32())
            {
                case (int)Keys.F1:
                    Console.WriteLine("Pressed F1");
                    break;
                case (int)Keys.F2:
                    Console.WriteLine("Pressed F2");
                    break;
                case (int)Keys.F3:
                    Console.WriteLine("Pressed F3");
                    break;
            }
        }
        base.WndProc(ref m);
    }
    just thought I would share how I got my answer
    Last edited by holthelper; 05-04-13 at 02:10 PM.




Advertisement