csharp/C1rdec/Poe-Lurker/src/Lurker/Helpers/KeyboardHelper.cs

KeyboardHelper.cs
//-----------------------------------------------------------------------
// 
//     Copyright © Wohs Inc.
// 
//-----------------------------------------------------------------------

namespace Lurker.Helpers
{
    using System;
    using System.Diagnostics;
    using System.Threading.Tasks;
    using Lurker.Extensions;
    using WindowsInput;
    using WindowsInput.Events;

    /// 
    /// Represents the keyboard helper.
    /// 
    public clast KeyboardHelper
    {
        #region Fields

        private Process _process;
        private IntPtr _windowHandle;

        #endregion

        #region Constructors

        /// 
        /// Initializes a new instance of the  clast.
        /// 
        /// The process identifier.
        public KeyboardHelper(int processId)
        {
            // For the settings instance
            if (processId != 0)
            {
                this._process = ProcessLurker.GetProcessById(processId);
                if (this._process != null)
                {
                    this._windowHandle = this._process.GetWindowHandle();
                }
            }
        }

        #endregion

        #region Methods

        /// 
        /// Waits for next key asynchronous.
        /// 
        /// The next key press.
        public Task WaitForNextKeyAsync()
        {
            var taskCompletionSource = new TaskCompletionSource();

            var hook = new Winook.KeyboardHook(ProcessLurker.CurrentProcessId);
            EventHandler handler = default;
            handler = (object s, Winook.KeyboardMessageEventArgs e) =>
            {
                if (e.Direction == Winook.KeyDirection.Up)
                {
                    taskCompletionSource.SetResult(e);
                    hook.MessageReceived -= handler;
                    hook.Dispose();
                }
            };

            hook.MessageReceived += handler;
            hook.InstallAsync().Wait();

            return taskCompletionSource.Task;
        }

        /// 
        /// Writes the specified text.
        /// 
        /// The text.
        /// The task awaiter.
        public async Task Write(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            await Simulate.Events().Click(text).Invoke();
        }

        /// 
        /// Simulates a search using Ctrl+F.
        /// 
        /// The search term to use.
        /// The task awaiter.
        public async Task Search(string searchTerm)
        {
            await Simulate.Events().Click(KeyCode.LMenu).Invoke();
            Native.SetForegroundWindow(this._windowHandle);

            var eventBuilder = Simulate.Events();
            eventBuilder.ClickChord(KeyCode.LControl, KeyCode.F);
            eventBuilder.Click(searchTerm);
            eventBuilder.Click(KeyCode.Return);
            await eventBuilder.Invoke();
        }

        /// 
        /// Sends the command.
        /// 
        /// The command.
        /// 
        /// The task awaiter.
        /// 
        protected async Task SendCommand(string command)
        {
            // This is to fix the first SetForegroundWindow
            await Simulate.Events().Click(KeyCode.LMenu).Invoke();
            Native.SetForegroundWindow(this._windowHandle);
            await Task.Delay(10);
            var foregroundWindow = Native.GetForegroundWindow();
            if (this._windowHandle != foregroundWindow)
            {
                await Task.Delay(100);
            }

            var eventBuilder = Simulate.Events();
            eventBuilder.Click(KeyCode.Return);
            eventBuilder.ClickChord(KeyCode.LControl, KeyCode.A);
            eventBuilder.Click(command);
            eventBuilder.Click(KeyCode.Return);

            await eventBuilder.Invoke();
        }

        #endregion
    }
}