Shortcut.HotkeyBinder.Bind C# (CSharp) Method

Bind() public method

Binds a Hotkey to a HotkeyCallback.
public Bind ( System.Windows.Forms.Hotkey hotkeyCombo ) : HotkeyCallback
hotkeyCombo System.Windows.Forms.Hotkey
return HotkeyCallback
        public HotkeyCallback Bind(Hotkey hotkeyCombo)
        {
            if (hotkeyCombo == null)
                throw new ArgumentNullException("hotkeyCombo");

            HotkeyCallback callback = new HotkeyCallback();
            container.Add(hotkeyCombo, callback);
            RegisterHotkey(hotkeyCombo);

            return callback;
        }

Same methods

HotkeyBinder::Bind ( Modifiers modifiers, Keys keys ) : HotkeyCallback

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