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