Shortcut.HotkeyBinder.IsHotkeyAlreadyBound C# (CSharp) Method

IsHotkeyAlreadyBound() public method

Indicates whether a Hotkey has been bound already either by this application or another application.
public IsHotkeyAlreadyBound ( System.Windows.Forms.Hotkey hotkeyCombo ) : bool
hotkeyCombo System.Windows.Forms.Hotkey /// The to evaluate. ///
return bool
        public bool IsHotkeyAlreadyBound(Hotkey hotkeyCombo)
        {
            bool successful =
                NativeMethods.RegisterHotKey(
                    hotkeyWindow.Handle,
                    hotkeyCombo.GetHashCode(),
                    (uint)hotkeyCombo.Modifier,
                    (uint)hotkeyCombo.Key);

            if (!successful)
                return true;

            NativeMethods.UnregisterHotKey(
                hotkeyWindow.Handle,
                hotkeyCombo.GetHashCode());

            return false;
        }

Usage Example

Exemplo n.º 1
0
        private static void BindHotkeys()
        {
            _binder = new HotkeyBinder();
            
            var hotkeySettings = new Dictionary<Type, SettingType>
            {
                [typeof(UserSelectionTemplate)] = SettingType.SelectAreaHotkey,
                [typeof(SelectWindowTemplate)] = SettingType.SelectWindowHotkey,
                [typeof(FullScreenTemplate)] = SettingType.FullscreenHotkey
            };

            var failedHotkeys = new List<SettingType>();
            foreach (var hotkeySetting in hotkeySettings)
            {
                var hotkey = Settings.GetSetting<Hotkey>(hotkeySetting.Value);
                if (_binder.IsHotkeyAlreadyBound(hotkey))
                {
                    failedHotkeys.Add(hotkeySetting.Value);
                    _log.Warn($"Hotkey {hotkeySetting.Value} is already bound. Therefor the hotkey will not be set.");
                    continue;
                }

                _binder.Bind(hotkey, args => HandleHotkey(hotkeySetting.Key));
            }

            if (failedHotkeys.SequenceEqual(hotkeySettings.Select(s => s.Value)))
            {
                _log.Fatal("All hotkeys failed to bind. Exiting..");
                NotificationProvider.Show("Lensert Closing", "All hotkeys failed to bind");
                Environment.Exit(0);
            }
            else if (failedHotkeys.Any())
            {
                var message = $"Failed to bind: {string.Join(", ", failedHotkeys)}";
                NotificationProvider.Show("Error", message, Util.OpenLog);
            }
        }