Ejemplo n.º 1
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.º 2
0
        private void MouseInfoProvider_OnMouseDown(object sender, System.Drawing.Point p)
        {
            if (ColorString != null)
            {
                // nasty hack - sometimes clipboard can be in use and it will raise and exception
                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        Clipboard.SetText(ColorString.ToLowerInvariant());
                        break;
                    }
                    catch (COMException ex)
                    {
                        const uint CLIPBRD_E_CANT_OPEN = 0x800401D0;
                        if ((uint)ex.ErrorCode != CLIPBRD_E_CANT_OPEN)
                        {
                            Logger.LogError("Failed to set text into clipboard", ex);
                        }
                    }
                    System.Threading.Thread.Sleep(10);
                }
            }

            _appStateHandler.HideColorPicker();
        }
Ejemplo n.º 3
0
        private void MouseInfoProvider_OnLeftMouseDown(object sender, System.Drawing.Point p)
        {
            if (ColorString != null)
            {
                ClipboardHelper.CopyIntoClipboard(ColorString);
                _userSettings.AddColorIntoHistory(_currentColor);
            }

            _appStateHandler.HideColorPicker();
        }
Ejemplo n.º 4
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();
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var virtualCode = e.KeyboardData.VirtualCode;

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

            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.º 6
0
        private void MouseInfoProvider_OnMouseDown(object sender, System.Drawing.Point p)
        {
            string colorRepresentationToCopy = string.Empty;

            switch (_userSettings.CopiedColorRepresentation.Value)
            {
                case ColorRepresentationType.HEX:
                    colorRepresentationToCopy = HexColor;
                    break;
                case ColorRepresentationType.RGB:
                    colorRepresentationToCopy = RgbColor;
                    break;
                default:
                    break;
            }

            CopyToClipboard(colorRepresentationToCopy);

            _appStateHandler.HideColorPicker();
            PowerToysTelemetry.Log.WriteEvent(new ColorPickerShowEvent());
        }