Ejemplo n.º 1
0
        private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var virtualCode = e.KeyboardData.VirtualCode;
            var name        = Helper.GetKeyName((uint)virtualCode);

            // we got modifier with additional info such as "Ctrl (left)" - get rid of parenthesess
            if (name.IndexOf("(", StringComparison.OrdinalIgnoreCase) > 0 && name.Length > 1)
            {
                name = name.Substring(0, name.IndexOf("(", StringComparison.OrdinalIgnoreCase)).Trim();
            }

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyDown)
            {
                if (!_currentlyPressedKeys.Contains(name))
                {
                    _currentlyPressedKeys.Add(name);
                }
            }
            else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyUp)
            {
                if (_currentlyPressedKeys.Contains(name))
                {
                    _currentlyPressedKeys.Remove(name);
                }
            }

            _currentlyPressedKeys.Sort();

            if (ArraysAreSame(_currentlyPressedKeys, _activationKeys))
            {
                _appStateHandler.ShowColorPicker();
                _currentlyPressedKeys.Clear();
            }
        }
Ejemplo n.º 2
0
        private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var currentlyPressedKeys = new List <string>();
            var virtualCode          = e.KeyboardData.VirtualCode;

            // ESC pressed
            if (virtualCode == KeyInterop.VirtualKeyFromKey(Key.Escape))
            {
                _appStateHandler.HideColorPicker();
                PowerToysTelemetry.Log.WriteEvent(new ColorPickerCancelledEvent());
                return;
            }

            var name = Helper.GetKeyName((uint)virtualCode);

            // If the last key pressed is a modifier key, then currentlyPressedKeys cannot possibly match with _activationKeys
            // because _activationKeys contains exactly 1 non-modifier key. Hence, there's no need to check if `name` is a
            // modifier key or to do any additional processing on it.

            // Check pressed modifier keys.
            AddModifierKeys(currentlyPressedKeys);

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyDown)
            {
                currentlyPressedKeys.Add(name);
            }

            currentlyPressedKeys.Sort();

            if (ArraysAreSame(currentlyPressedKeys, _activationKeys))
            {
                _appStateHandler.ShowColorPicker();
            }
        }
Ejemplo n.º 3
0
        private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var virtualCode = e.KeyboardData.VirtualCode;

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyDown)
            {
                if (!_currentlyPressedKeys.Contains(virtualCode))
                {
                    _currentlyPressedKeys.Add(virtualCode);
                }
            }
            else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyUp)
            {
                if (_currentlyPressedKeys.Contains(virtualCode))
                {
                    _currentlyPressedKeys.Remove(virtualCode);
                }
            }

            _currentlyPressedKeys.Sort();

            if (ArraysAreSame(_currentlyPressedKeys, _activationKeys))
            {
                _appStateHandler.ShowColorPicker();
            }

            if (_currentlyPressedKeys.Count == 1 && _currentlyPressedKeys[0] == 27)
            {
                _colorsHistoryWindowHelper.HideColorsHistory();
                _zoomWindowHelper.CloseZoomWindow();
            }
        }
Ejemplo n.º 4
0
        private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var virtualCode = e.KeyboardData.VirtualCode;

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyDown)
            {
                if (!_currentlyPressedKeys.Contains(virtualCode))
                {
                    _currentlyPressedKeys.Add(virtualCode);
                }
            }
            else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyUp)
            {
                if (_currentlyPressedKeys.Contains(virtualCode))
                {
                    _currentlyPressedKeys.Remove(virtualCode);
                }
            }

            _currentlyPressedKeys.Sort();

            if (ArraysAreSame(_currentlyPressedKeys, _activationKeys))
            {
                _appStateHandler.ShowColorPicker();
            }
        }
Ejemplo n.º 5
0
        private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var currentlyPressedKeys = new List <string>();
            var virtualCode          = e.KeyboardData.VirtualCode;

            // ESC pressed
            if (virtualCode == KeyInterop.VirtualKeyFromKey(Key.Escape))
            {
                _appStateHandler.HideColorPicker();
                PowerToysTelemetry.Log.WriteEvent(new ColorPickerCancelledEvent());
                return;
            }

            var name = Helper.GetKeyName((uint)virtualCode);

            // If the last key pressed is a modifier key, then currentlyPressedKeys cannot possibly match with _activationKeys
            // because _activationKeys contains exactly 1 non-modifier key. Hence, there's no need to check if `name` is a
            // modifier key or to do any additional processing on it.
            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown || e.KeyboardState == GlobalKeyboardHook.KeyboardState.SysKeyDown)
            {
                // Check pressed modifier keys.
                AddModifierKeys(currentlyPressedKeys);

                currentlyPressedKeys.Add(name);
            }

            currentlyPressedKeys.Sort();

            if (currentlyPressedKeys.Count == 0 && _previouslyPressedKeys.Count != 0)
            {
                // no keys pressed, we can enable activation shortcut again
                _activationShortcutPressed = false;
            }

            _previouslyPressedKeys = currentlyPressedKeys;

            if (ArraysAreSame(currentlyPressedKeys, _activationKeys))
            {
                // avoid triggering this action multiple times as this will be called nonstop while keys are pressed
                if (!_activationShortcutPressed)
                {
                    _activationShortcutPressed = true;
                    if (_userSettings.ActivationAction.Value == ColorPickerActivationAction.OpenEditor)
                    {
                        _appStateHandler.ShowColorPickerEditor();
                    }
                    else
                    {
                        _appStateHandler.ShowColorPicker();
                    }
                }
            }
        }