Ejemplo n.º 1
0
        public IKeyboardCommandTrigger OnEvent(IHotkey hotkey, KeyEvent keyEvent,
                                               string stateTree = KeyStateTrees.Default)
        {
            if (keyEvent == KeyEvent.Hit)
            {
                return(OnHit(hotkey, stateTree));
            }

            var sequence = hotkey switch
            {
                ISequenceUnit sequenceUnit => new List <ICombination>()
                {
                    sequenceUnit.ToCombination().ToCombination()
                },
                ISequence seq => seq.ToList(),
                _ => throw new Exception("IHotkey should be ISequence or ISequenceUnit")
            };
            var trigger = new KeyboardCommandTrigger();
            var metaKey = Add(sequence, keyEvent,
                              new KeyCommand(trigger.OnExecute)
            {
                CanExecute = trigger.OnCanExecute
            }, stateTree) as MetaKey;

            trigger._metaKey = metaKey;
            return(trigger);
        }
Ejemplo n.º 2
0
        public GenericHotkeyProxy(IHotkey hotkey)
        {
            if (hotkey is KeyboardHotkey kbdHotkey)
            {
                this.Type            = HotkeyType.Keyboard;
                this.keyboardHotkey  = new KeyboardHotkey(kbdHotkey);
                this.mouseHookHotkey = new MouseHookHotkey(hotkey);
            }
            else if (hotkey is MouseHookHotkey mhHotkey)
            {
                this.Type            = HotkeyType.MouseHook;
                this.mouseHookHotkey = new MouseHookHotkey(mhHotkey);
                this.keyboardHotkey  = new KeyboardHotkey(hotkey);
            }
            else
            {
                this.Type            = HotkeyType.Undefined;
                this.keyboardHotkey  = hotkey != null ? new KeyboardHotkey(hotkey) : new KeyboardHotkey();
                this.mouseHookHotkey = hotkey != null ? new MouseHookHotkey(hotkey) : new MouseHookHotkey();
            }

            App.Container.BuildUp(this.keyboardHotkey, this.mouseHookHotkey);

            this.keyboardHotkey.PropertyChanged  += this.KeyboardHotkey_PropertyChanged;
            this.mouseHookHotkey.PropertyChanged += this.MouseHookHotkey_PropertyChanged;
        }
Ejemplo n.º 3
0
        public static Task <IKeyEventArgs> UpAsync(this IHotkey sequenceUnit, int timeout = -1, string description = "", string stateTree = KeyStateTrees.Default)
        {
            var command = new KeyEventAsync();

            sequenceUnit.OnUp(command.OnEvent, null, description, stateTree);
            return(command.WaitAsync(timeout));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// register the key to the state tree, and wait the down event;
        /// timeout: return null
        /// </summary>
        public static Task <IKeyEventArgs> DownAsync(this IHotkey hotkey, int timeout = -1, string description = "", string stateTree = KeyStateTrees.Default)
        {
            var command = new KeyEventAsync();

            hotkey.OnDown(command.OnEvent, null, description, stateTree);
            return(command.WaitAsync(timeout));
        }
Ejemplo n.º 5
0
        public ISequence Then(IHotkey hotkey)
        {
            var sequence = new Sequence(this);

            sequence.AddRange(hotkey.ToSequence());
            return(sequence);
        }
Ejemplo n.º 6
0
 private void CheckIfProperHotkeyType(Type t, IHotkey hk)
 {
     if (t != hk.GetType())
     {
         throw new Exception("Invalid hotkey type");
     }
 }
Ejemplo n.º 7
0
        public void ModifyAudioHotkey(int hotkeyId, string newName = "", string newKey = "", string newFiles = "", float newVolume = -1, int newStartingTime = -1, string newAudioDevice = "")
        {
            IHotkey hk = FindHotkeyById(hotkeyId);

            CheckIfProperHotkeyType(typeof(AudioHotkey), hk);
            AudioHotkey hotkey = (AudioHotkey)hk;

            if (newName != "")
            {
                hotkey.Name = newName;
            }
            if (newKey != "")
            {
                ModifyHotkey(hotkeyId, newKey);
            }
            if (newFiles != "")
            {
                hotkey.Files = newFiles;
            }
            if (newVolume != -1)
            {
                hotkey.Volume = newVolume / 100;
            }
            if (newStartingTime != -1)
            {
                hotkey.StartingTime = newStartingTime;
            }
            if (newAudioDevice != "")
            {
                hotkey.AudioDevice = new Guid(newAudioDevice);
            }
        }
Ejemplo n.º 8
0
 public static void Remove(IHotkey hotkey)
 {
     if (HotkeyCollection.Contains(hotkey))
     {
         HotkeyCollection.Remove(hotkey);
     }
 }
Ejemplo n.º 9
0
 public static void Add(IHotkey hotkey)
 {
     if (!HotkeyCollection.Contains(hotkey))
     {
         HotkeyCollection.Add(hotkey);
     }
 }
Ejemplo n.º 10
0
        public static IKeyCommand OnHit(this IHotkey sequenceUnit, Action <IKeyEventArgs> execute,
                                        Predicate <IKeyEventArgs> canExecute = null, string description = "", string stateTree = KeyStateTrees.Default)
        {
            var trigger = Keyboard.OnHit(sequenceUnit, stateTree);

            return(trigger.Register(execute, canExecute, description));
        }
Ejemplo n.º 11
0
        public IHotkey FindHotkeyById(int id)
        {
            IHotkey hk = null;

            hk = hotkeysList.Find(hkey => hkey.Id == id);
            return(hk);
        }
Ejemplo n.º 12
0
        public IKeyboardCommandTrigger OnHit(IHotkey hotkey, string stateTree = KeyStateTrees.Default)
        {
            var trigger = new KeyboardCommandTrigger();
            var token   = Hit(hotkey,
                              trigger.OnExecute, trigger.OnCanExecute, "", stateTree) as KeyCommandTokens;

            trigger._metaKey = token?.metaKey;
            return(trigger);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// down up happened successively
        /// </summary>
        private IKeyCommand Hit(IHotkey hotkey, Action <IKeyEventArgs> execute,
                                Predicate <IKeyEventArgs> canExecute = null, string description = "",
                                string stateTree = KeyStateTrees.Default)
        {
            var noEventTimer = new NoEventTimer();
            // state
            var           handling     = false;
            IKeyEventArgs keyDownEvent = null;

            void Reset()
            {
                handling     = false;
                keyDownEvent = null;
            }

            var token = new KeyCommandTokens
            {
                hotkey.OnDown(e =>
                {
                    handling     = true;
                    keyDownEvent = e;
                }, e =>
                {
                    var noEventDuration = noEventTimer.NoEventDuration;
                    if (noEventDuration > StateResetTime)
                    {
                        Reset();
                    }
                    noEventTimer.EventPulse();

                    return(canExecute?.Invoke(e) ?? true);
                }, description, stateTree),

                hotkey.OnUp(e =>
                {
                    if (!handling)
                    {
                        Console.WriteLine($"\t{hotkey}_Hit Down CanExecute:false");
                        return;
                    }

                    handling = false;

                    if (keyDownEvent == e.LastKeyDownEvent)
                    {
                        execute(e);
                    }
                    else
                    {
                        Console.WriteLine($"\t{hotkey}_Hit: last down event is not from me, Not Execute!");
                    }
                }, canExecute, description, stateTree)
            };

            return(token);
        }
Ejemplo n.º 14
0
 // if the handler async run, this is needed.
 public static IHotkey Handled(this IHotkey hotkey,
                               KeyEvent keyEvent = KeyEvent.All)
 {
     hotkey.Handled = keyEvent;
     // if ((keyEvent & KeyEvent.Down) == KeyEvent.Down)
     //     hotkey.Down(e => e.Handled = true);
     // if ((keyEvent & KeyEvent.Up) == KeyEvent.Up)
     //     hotkey.Up(e => e.Handled = true);
     // if ((keyEvent & KeyEvent.AllUp) == KeyEvent.AllUp)
     //     hotkey.AllUp(e => e.Handled = true);
     return(hotkey);
 }
Ejemplo n.º 15
0
 public void RemoveHotkey(int hotkeyId)
 {
     if (hotkeyId == MasterHotkey.Id)
     {
         MasterHotkey.Unregister();
     }
     else
     {
         IHotkey hk = FindHotkeyById(hotkeyId);
         hk.Unregister();
         hotkeysList.Remove(hk);
     }
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Get the shortcut string for a hotkey, if it's defined
        /// </summary>
        /// <param name="hotkey"></param>
        /// <returns></returns>
        public string GetHotkeyString(IHotkey hotkey)
        {
            if (hotkey == null)
            {
                return(null);
            }
            var keys = _registeredHotkeys.Where(x => x.Value == hotkey).Select(x => x.Key).ToList();

            if (keys.Count == 0)
            {
                return(null);
            }

            return(keys.Contains(hotkey.DefaultHotkey) ? hotkey.DefaultHotkey : keys.FirstOrDefault());
        }
Ejemplo n.º 17
0
        public void ModifyHotkey(int hotkeyId, string newKey)
        {
            IHotkey         hk         = FindHotkeyById(hotkeyId);
            KeyAndModifiers newFullKey = CreateAndValidateFullKey(newKey);
            KeyAndModifiers oldFullKey = hk.Key;

            hk.Key = newFullKey;
            int result = hk.Reregister();

            if (result != 1 && result != -3)
            {
                hk.Key = oldFullKey;
                hk.Register(mainFrm);
                CheckHotkeyResult(result, newKey);
            }
        }
Ejemplo n.º 18
0
        public CopypastaController(IClipboard clipboard, IClipboardHistoryManager clipboardHistoryManager,
                                   IClipboardBindingManager clipboardBindingManager, IHotkey ctrlVHotkey, IHotkey ctrlCHotkey, IHotkey escHotkey,
                                   IKeyTracker keyTracker, IInputSimulator inputSimulator, INotificationDispatcher notificationDispatcher)
        {
            _clipboard = clipboard;
            _clipboardHistoryManager = clipboardHistoryManager;
            _clipboardBindingManager = clipboardBindingManager;
            _ctrlVHotkey             = ctrlVHotkey;
            _ctrlCHotkey             = ctrlCHotkey;
            _escHotkey              = escHotkey;
            _keyTracker             = keyTracker;
            _inputSimulator         = inputSimulator;
            _notificationDispatcher = notificationDispatcher;
            _copypastaStateMachine  = new StateMachine <CopypastaState, CopypastaTrigger>(CopypastaState.Idle);
            _keyPressedTrigger      = _copypastaStateMachine.SetTriggerParameters <Key>(CopypastaTrigger.KeyPressed);

            ConfigureCopypastaStateMachine();
            ConfigureEventTriggers();
        }
Ejemplo n.º 19
0
        internal bool Contains(IHotkey hotKey)
        {
            IEnumerable <KeyEventCommand> values = null;

            switch (hotKey)
            {
            case ISequenceUnit k:
                values = _trie.Get(new List <ICombination>(k.ToCombination()));
                break;

            case ISequence s:
                values = _trie.Get(s.ToList());
                break;

            default: throw new Exception("not supported!");
            }

            return(values.Any());
        }
Ejemplo n.º 20
0
 public ISequence Then(IHotkey hotkey)
 {
     this.AddRange(hotkey.ToSequence());
     return(this);
 }
Ejemplo n.º 21
0
 /// <inheritdoc />
 public MouseHookHotkey([NotNull] IHotkey hotkey) : base(hotkey)
 {
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Add a hotkey to the list but do not register it
 /// </summary>
 /// <param name="hotkey">The hotkey to add</param>
 private void Add(IHotkey hotkey)
 {
     _hotkeys[hotkey.ID] = hotkey;
 }
Ejemplo n.º 23
0
 /// <inheritdoc />
 public KeyboardHotkey([NotNull] IHotkey hotkey) : base(hotkey)
 {
 }
Ejemplo n.º 24
0
 public FakeHotkey([NotNull] IHotkey hotkey) : base(hotkey)
 {
 }
Ejemplo n.º 25
0
 public IKeyboardCommandTrigger OnAllUp(IHotkey hotkey, string stateTree = KeyStateTrees.Default)
 {
     return(OnEvent(hotkey, KeyEvent.AllUp, stateTree));
 }
Ejemplo n.º 26
0
 public static IKeyCommand HardMap(this IHotkey key, Key target, Predicate <IKeyEventArgs> canExecute = null)
 {
     return(Keyboard.HardMap(key, new Combination(target), canExecute));
 }
Ejemplo n.º 27
0
 public static IKeyCommand MapOnDownUp(this IHotkey key, ISequenceUnit target, Predicate <IKeyEventArgs> canExecute = null)
 {
     return(Keyboard.MapOnDownUp(key, target, canExecute));
 }
Ejemplo n.º 28
0
 public ISequence Then(IHotkey hotkey) => new Combination(this).Then(hotkey);
 /// <summary>
 /// Updates the active hotkey value.
 /// </summary>
 /// <param name="hotkey">The new active hotkey.</param>
 private void UpdateActiveValue(IHotkey hotkey)
 {
     this.ActiveHotkey = hotkey;
 }