public HotkeyStatus RegisterHotkey(Keys hotkey, Action hotkeyPress = null, int tag = 0) { KeyInfo keyInfo = new KeyInfo(hotkey); if (keyInfo.KeyCode == Keys.None) { return(HotkeyStatus.NotConfigured); } if (IsHotkeyExist(hotkey)) { DebugHelper.WriteLine("Hotkey already exist: " + keyInfo); return(HotkeyStatus.Failed); } string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + (int)hotkey; ushort id = NativeMethods.GlobalAddAtom(atomName); if (id == 0) { DebugHelper.WriteLine("Unable to generate unique hotkey ID. Error code: " + Marshal.GetLastWin32Error()); return(HotkeyStatus.Failed); } if (!NativeMethods.RegisterHotKey(Handle, (int)id, (uint)keyInfo.ModifiersEnum, (uint)keyInfo.KeyCode)) { NativeMethods.GlobalDeleteAtom(id); DebugHelper.WriteLine("Unable to register hotkey: {0}\r\nError code: {1}", keyInfo, Marshal.GetLastWin32Error()); return(HotkeyStatus.Failed); } HotkeyInfo hotkeyInfo = new HotkeyInfo(id, hotkey, hotkeyPress, tag); HotkeyList.Add(hotkeyInfo); return(HotkeyStatus.Registered); }