Esempio n. 1
0
        /// <summary>
        /// Release pressed keys and return a function which will re-press them again.
        /// </summary>
        /// <returns>Function which will re-press them again.</returns>
        private async Task <Func <Task> > ResetPressedAsync()
        {
            TypoLogging.Write(_pressed?.Count);
            if (_pressed == null)
            {
                return(null);
            }

            var pressed = _pressed.ToArray();

            for (var i = pressed.Length - 1; i >= 0; i--)
            {
                TypoLogging.Write("Unpress: " + pressed[i]);
                User32.SendInput(new User32.KeyboardInput {
                    ScanCode  = ToScanCode(pressed[i]),
                    Flags     = User32.KeyboardFlag.ScanCode | User32.KeyboardFlag.KeyUp,
                    ExtraInfo = KeyboardListener.AppEventFlag
                });
                await Task.Delay(Delay);
            }

            return(async() => {
                for (var i = 0; i < pressed.Length; i++)
                {
                    TypoLogging.Write("Re-press: " + pressed[i]);
                    User32.SendInput(new User32.KeyboardInput {
                        ScanCode = ToScanCode(pressed[i]),
                        Flags = User32.KeyboardFlag.ScanCode,
                        ExtraInfo = KeyboardListener.AppEventFlag
                    });
                    await Task.Delay(Delay);
                }
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Gets selected/copied/all text, passes it through provided callback and puts it back.
        /// </summary>
        /// <param name="proc">Callback converting text.</param>
        /// <param name="type">What text to replace: selected/copied/all/auto.</param>
        /// <param name="cancellation">Cancellation token.</param>
        public async Task ReplaceAsync(ReplaceTextCallback proc, TextReplacementType type, CancellationToken cancellation)
        {
            using (type == TextReplacementType.Clipboard ? null : Backup()) {
                switch (type)
                {
                case TextReplacementType.Auto:
                    await PressAsync(Keys.RControlKey, Keys.C);

                    if (!Clipboard.ContainsText())
                    {
                        await PressAsync(Keys.RControlKey, Keys.A);
                        await PressAsync(Keys.RControlKey, Keys.C);
                    }
                    break;

                case TextReplacementType.All:
                    await PressAsync(Keys.RControlKey, Keys.A);
                    await PressAsync(Keys.RControlKey, Keys.C);

                    break;

                case TextReplacementType.Selected:
                    await PressAsync(Keys.RControlKey, Keys.C);

                    break;
                }

                if (cancellation.IsCancellationRequested)
                {
                    return;
                }

#if DEBUG
                var s = Stopwatch.StartNew();
#endif

                try {
                    var replacement = await proc(GetClipboardText(), cancellation);

                    if (cancellation.IsCancellationRequested)
                    {
                        return;
                    }

                    SetClipboardText(replacement);
                } catch (Exception e) {
                    TypoLogging.NonFatalErrorNotify("Can’t execute script", null, e);
                }

#if DEBUG
                TypoLogging.Write($"{s.Elapsed.TotalMilliseconds:F1} ms");
#endif

                if (type != TextReplacementType.Clipboard)
                {
                    await PressAsync(Keys.RControlKey, Keys.V);
                }
            }
        }
Esempio n. 3
0
 private void InitializeListener()
 {
     TypoLogging.Write("Here");
     _pressed  = new List <Keys>();
     _keyboard = new KeyboardListener();
     _keyboard.PreviewKeyDown += OnPreviewKeyDown;
     _keyboard.PreviewKeyUp   += OnPreviewKeyUp;
     _timer = new DispatcherTimer {
         Interval = TimeSpan.FromSeconds(10)
     };
     _timer.Tick += OnTimerTick;
 }
Esempio n. 4
0
 private static string GetClipboardText()
 {
     try {
         if (!Clipboard.ContainsText())
         {
             return(null);
         }
         var data = Clipboard.GetText();
         return(string.IsNullOrEmpty(data) ? null : data);
     } catch (Exception e) {
         TypoLogging.Write(e);
         return(null);
     }
 }
Esempio n. 5
0
 private static void SetClipboardTextInner([CanBeNull] string data)
 {
     try {
         if (string.IsNullOrEmpty(data))
         {
             Clipboard.Clear();
         }
         else
         {
             Clipboard.SetDataObject(data, true, 5, 100);
         }
     } catch (Exception e) {
         TypoLogging.Write(e);
     }
 }