C # RegHotKey class. It is easier to write window programs and register global hotkeys.

Source: Internet
Author: User

C # it is inconvenient to operate winApi. For example, without pointers, It is a headache.

In the past two days, you need to write a program to register the global hotkey, So you wrote this class. Registering hotkeys is easier.

The call is very simple:

Hk = new HotKey ();
Hk. RegHotKey (Keys. End, fun); // The method name is the 2nd parameter.

Hk. UnRegHotKey (Keys. End); // cancel the hotkey

using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Windows.Forms;namespace xinjeHotkey{        public class HotKey:Form    {        private List
 
   keyIds = new List
  
   ();        private uint id = 0;        [DllImport("user32.dll")]        public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);                [DllImport("user32.dll")]        public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);         public HotKey()        {                              }        public delegate void myfun();        public void RegHotKey(Keys key,myfun e)        {            id++;            idAndFun fi = new idAndFun();            fi.id = id;            fi.key = (uint)key;            fi.mf = (object)e;            keyIds.Add(fi);            RegisterHotKey(this.Handle, id, 0, (uint)key);        }        public void UnRegHotKey(Keys key)        {            uint temp = (uint)key;            for (int i = 0; i < keyIds.Count; i++)            {                if (keyIds[i].key == temp)                {                    UnregisterHotKey(this.Handle, keyIds[i].id);                    keyIds.RemoveAt(i);                }            }        }        protected override void WndProc(ref Message m)        {            const int WM_HOTKEY = 0x0312;            if (m.Msg == WM_HOTKEY)            {                for (int i = 0; i < keyIds.Count; i++)                {                    if (m.WParam.ToInt32() == keyIds[i].id)                    {                        myfun mff = (myfun)keyIds[i].mf;                        mff();                    }                }            }            base.WndProc(ref m);        }    }    class idAndFun    {        public uint id;        public uint key;        public object mf;    }}
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.