Exemplo n.º 1
0
        public static async Task <bool> ActivateProcess(string processName, CancellationToken token)
        {
            var hwnd = Utilities.FindProcessWindow(processName);

            if (hwnd == IntPtr.Zero)
            {
                return(false);
            }

            var placement = new WinApi.WINDOWPLACEMENT();

            placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement);
            WinApi.GetWindowPlacement(hwnd, ref placement);
            var showCmd = placement.showCmd switch
            {
                WinApi.SW_SHOWMAXIMIZED => WinApi.SW_SHOWMAXIMIZED,
                WinApi.SW_SHOWMINIMIZED => WinApi.SW_RESTORE,
                _ => WinApi.SW_SHOWNORMAL
            };

            using (AttachedThreadInputScope.Create())
            {
                await InputHelpers.TimerYield(token : token);

                WinApi.SetForegroundWindow(hwnd);
                WinApi.ShowWindow(hwnd, showCmd);
                await InputHelpers.TimerYield(token : token);
            }
            return(true);
        }
        public static async Task <bool> FeedTextAsync(string text, CancellationToken token)
        {
            await s_asyncLock.WaitAsync(token);

            try
            {
                var foregroundWindow = WinApi.GetForegroundWindow();
                WinApi.BlockInput(true);
                try
                {
                    var size  = Marshal.SizeOf <WinApi.INPUT>();
                    var input = new WinApi.INPUT[1];

                    // feed each character individually and asynchronously
                    foreach (var c in text)
                    {
                        token.ThrowIfCancellationRequested();

                        if (WinApi.GetForegroundWindow() != foregroundWindow ||
                            IsAnyKeyPressed())
                        {
                            break;
                        }

                        if (c == '\n' || c == '\r')
                        {
                            // we need this for correctly handling line breaks
                            // when pasting into Chromium's <textarea>
                            SimulateKeyDown(WinApi.VK_RETURN);
                            SimulateKeyUp(WinApi.VK_RETURN);
                        }
                        else
                        {
                            CharToKeyboardInput(c, ref input[0]);
                            if (WinApi.SendInput((uint)input.Length, input, size) == 0)
                            {
                                break;
                            }
                        }

                        if (InputHelpers.AnyInputMessage(WinApi.QS_ALLINPUT))
                        {
                            await InputHelpers.TimerYield(token : token);
                        }
                    }
                    return(true);
                }
                finally
                {
                    WinApi.BlockInput(false);
                }
            }
            finally
            {
                s_asyncLock.Release();
            }
        }
Exemplo n.º 3
0
        public static async Task <bool> FeedTextAsync(string text, CancellationToken token)
        {
            await s_asyncLock.WaitAsync(token);

            try
            {
                var foregroundWindow = WinApi.GetForegroundWindow();
                WinApi.BlockInput(true);
                try
                {
                    var size  = Marshal.SizeOf <WinApi.INPUT>();
                    var input = new WinApi.INPUT[1];

                    // feed each character individually and asynchronously
                    foreach (var c in text)
                    {
                        token.ThrowIfCancellationRequested();

                        if (WinApi.GetForegroundWindow() != foregroundWindow ||
                            IsAnyKeyPressed())
                        {
                            break;
                        }

                        CharToKeyboardInput(c, ref input[0]);
                        if (WinApi.SendInput((uint)input.Length, input, size) == 0)
                        {
                            break;
                        }

                        if (InputHelpers.AnyInputMessage(WinApi.QS_ALLINPUT))
                        {
                            await InputHelpers.TimerYield(token : token);
                        }
                    }
                    return(true);
                }
                finally
                {
                    WinApi.BlockInput(false);
                }
            }
            finally
            {
                s_asyncLock.Release();
            }
        }