Exemple #1
0
    public async Task DefaultTest()
    {
        using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
        var cancellationToken = cancellationTokenSource.Token;

        using var hook = new LowLevelMouseHook().WithEventLogging();

        hook.Start();

        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
    }
Exemple #2
0
    public async Task HandlingTest()
    {
        using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
        var cancellationToken = cancellationTokenSource.Token;

        using var hook = new LowLevelMouseHook
              {
                  Handling = true,
              }.WithEventLogging();
        hook.Move += (_, args) => args.IsHandled = true;
        hook.Down += (_, args) => args.IsHandled = true;
        hook.Up   += (_, args) => args.IsHandled = true;

        hook.Start();

        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
    }
Exemple #3
0
    public async Task AddKeyboardKeysTest()
    {
        using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
        var cancellationToken = cancellationTokenSource.Token;

        using var hook = new LowLevelMouseHook
              {
                  AddKeyboardKeys        = true,
                  IsLeftRightGranularity = true,
                  IsCapsLock             = true,
                  IsExtendedMode         = true,
              }.WithEventLogging();

        hook.Start();

        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
    }
Exemple #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="process"></param>
        /// <param name="cancellationToken"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public async Task <Rectangle> SelectAsync(
            IProcess <ICommand> process,
            CancellationToken cancellationToken = default)
        {
            process = process ?? throw new ArgumentNullException(nameof(process));

            if (Window == null)
            {
                await InitializeAsync(cancellationToken).ConfigureAwait(false);
            }

            Window = Window ?? throw new InvalidOperationException("Window is null.");

            var scaleFactor = await Dispatcher.InvokeAsync(
                () => Window.GetDpi(),
                DispatcherPriority.Normal,
                cancellationToken);

            var startPoint   = new Point();
            var endPoint     = new Point();
            var currentPoint = new Point();

            using var exceptions = new ExceptionsBag();
            using var hook       = new LowLevelMouseHook
                  {
                      GenerateMouseMoveEvents = true,
                  };
            hook.ExceptionOccurred += (_, exception) =>
            {
                // ReSharper disable once AccessToDisposedClosure
                exceptions.OnOccurred(exception);
            };

            var isInitialized = false;

            hook.Move += (_, args) =>
            {
                currentPoint = args.Position;
                if (isInitialized)
                {
                    return;
                }

                startPoint    = currentPoint.ToApp(scaleFactor);
                endPoint      = startPoint;
                isInitialized = true;
            };
            hook.Start();

            using var timer = new Timer(15);
            timer.Elapsed  += (_, _) =>
            {
                if (!isInitialized)
                {
                    return;
                }

                endPoint = currentPoint.ToApp(scaleFactor);

                Dispatcher.Invoke(() =>
                {
                    ApplyRectangle(
                        Window,
                        startPoint,
                        endPoint);
                });
            };
            timer.Start();

            await Dispatcher.InvokeAsync(() =>
            {
                ApplyRectangle(
                    Window,
                    startPoint,
                    endPoint);
                Window.Border.Visibility = Visibility.Visible;
            }, DispatcherPriority.Normal, cancellationToken);

            await process.WaitAsync(cancellationToken).ConfigureAwait(false);

            timer.Dispose();
            hook.Dispose();

            await Dispatcher.InvokeAsync(() =>
            {
                Window.Border.Visibility = Visibility.Hidden;
            }, DispatcherPriority.Normal, cancellationToken);

            return(startPoint
                   .ToRectangle(endPoint)
                   .Normalize()
                   .ToPhysical(scaleFactor));
        }