Ejemplo n.º 1
0
        private HotKeyData CreateHotKeyData(
            string id,
            string description,
            bool global,
            HotKey defaultHotKey,
            Action callback,
            bool enabled)
        {
            HotKey actualHotKey = null;

            if (_userHotKeyMap.ContainsKey(id))
            {
                actualHotKey = _userHotKeyMap[id];
            }

            else if (_hotKeyDataMap.ContainsKey(defaultHotKey) == false)
            {
                actualHotKey = defaultHotKey;
            }

            var hkData = new HotKeyData(
                id,
                description,
                enabled,
                global,
                defaultHotKey,
                actualHotKey,
                callback);

            hkData.HotKeyChanged += HotKeyChanged;

            return(hkData);
        }
        private void HotKeyChanged(
            HotKeyData hkData,
            HotKey actualBefore,
            HotKey actualAfter)
        {
            if (actualBefore != null)
            {
                _hotKeyDataMap.TryRemove(actualBefore, out _);
                _userHotKeyMap.Reverse.Remove(actualBefore);

                if (hkData.IsGlobal)
                {
                    _kbHookSvc.UnregisterHotKey(actualBefore);
                }
            }

            if (actualAfter != null)
            {
                if (_hotKeyDataMap.ContainsKey(actualAfter))
                {
                    throw new ArgumentException("Hotkey already used");
                }

                // Remap hotkey
                _hotKeyDataMap[actualAfter] = hkData;

                // Remove ghost user hotkey if necessary, update hotkey
                _userHotKeyMap.Reverse.Remove(actualAfter);
                _userHotKeyMap[hkData.Id] = actualAfter;

                if (hkData.IsGlobal)
                {
                    _kbHookSvc.RegisterHotKey(actualAfter, hkData.Callback);
                }
            }

            LogTo.Debug($"Hotkey {hkData.Id} is now bound to {actualAfter}.");

            _delayedTask.Trigger(1000);
        }