public void RegisterHotKey(string Purpose, KeyModifiers modifiers, Keys vk, Action <HotKey> OnPressed) { HotKey key = RegisteredHotKeys.SingleOrDefault(o => o.Purpose == Purpose); if (key == null) { key = new HotKey() { Purpose = Purpose, id = id, fsModifiers = modifiers, vk = vk, OnPressed = OnPressed }; bool issuccess = WinApiMethods.RegisterHotKey(windowHandle, key.id, key.fsModifiers, (uint)key.vk); if (!issuccess) { key = null; Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error()); if (ex.Message == "Hot key is already registered") { throw ex; } } else { RegisteredHotKeys.Add(key); id++; } } else { throw new AlreadyMappedException(key); } }
public void ReplaceHotKey(string Purpose, KeyModifiers modifiers, Keys vk, Action <HotKey> OnPressed) { HotKey key = RegisteredHotKeys.SingleOrDefault(o => o.Purpose == Purpose); if (key != null) { key.fsModifiers = modifiers; key.vk = vk; bool issuccess = WinApiMethods.RegisterHotKey(windowHandle, key.id, key.fsModifiers, (uint)key.vk); if (!issuccess) { key = null; Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error()); if (ex.Message == "Hot key is already registered") { throw ex; } } } else { throw new KeyNotFoundException(Purpose); } }