Esempio n. 1
0
        /// <summary>
        ///     Go to the previous "page"
        /// </summary>
        /// <returns>bool if this worked</returns>
        public bool Previous()
        {
            var        result = false;
            ScrollInfo scrollInfoBefore;
            var        hasScrollInfo = TryRetrievePosition(out scrollInfoBefore);

            switch (ScrollMode)
            {
            case ScrollModes.KeyboardPageUpDown:
                result = InputGenerator.KeyPress(VirtualKeyCodes.PRIOR) == 2;
                break;

            case ScrollModes.WindowsMessage:
                result = SendScrollMessage(ScrollBarCommands.SB_PAGEUP);
                break;

            case ScrollModes.AbsoluteWindowMessage:
                if (!hasScrollInfo)
                {
                    return(false);
                }
                // Calculate previous position, clone the scrollInfoBefore
                var scrollInfoForPrevious = scrollInfoBefore;
                scrollInfoForPrevious.Position = Math.Max(scrollInfoBefore.Minimum, scrollInfoBefore.Position - (int)scrollInfoBefore.PageSize);
                result = ApplyPosition(ref scrollInfoForPrevious);
                break;

            case ScrollModes.MouseWheel:
                var bounds      = ScrollingWindow.GetInfo().Bounds;
                var middlePoint = new NativePoint(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
                result = InputGenerator.MoveMouseWheel(WheelDelta, middlePoint) == 1;
                break;
            }
            return(result);
        }
Esempio n. 2
0
        private async Task TestInput()
        {
            // Start a process to test against
            using (var process = Process.Start("notepad.exe"))
            {
                // Make sure it's started
                Assert.NotNull(process);
                // Wait until the process started it's message pump (listening for input)
                process.WaitForInputIdle();

                // Find the belonging window
                var notepadWindow = await WindowsEnumerator.EnumerateWindowsAsync()
                                    .Where(interopWindow =>
                {
                    int processId;
                    User32Api.GetWindowThreadProcessId(interopWindow.Handle, out processId);
                    return(processId == process.Id);
                })
                                    .FirstOrDefaultAsync();

                Assert.NotNull(notepadWindow);

                // Send input
                var sentInputs = InputGenerator.KeyPress(VirtualKeyCodes.KEY_R, VirtualKeyCodes.KEY_O, VirtualKeyCodes.KEY_B, VirtualKeyCodes.KEY_I, VirtualKeyCodes.KEY_N);
                // Test if we indead sent 10 inputs (5 x down & up)
                Assert.Equal((uint)10, sentInputs);

                // Kill the process
                process.Kill();
            }
        }
        /// <summary>
        ///     Set the window as foreground window
        /// </summary>
        /// <param name="interopWindow">The window to bring to the foreground</param>
        /// <param name="workaround">bool with true to use a trick (press Alt) to really bring the window to the foreground</param>
        public static async Task ToForegroundAsync(this IInteropWindow interopWindow, bool workaround = true)
        {
            // Nothing we can do if it's not visible!
            if (!interopWindow.IsVisible())
            {
                return;
            }
            // Window is already the foreground window
            if (User32Api.GetForegroundWindow() == interopWindow.Handle)
            {
                return;
            }
            if (interopWindow.IsMinimized())
            {
                interopWindow.Restore();
                while (interopWindow.IsMinimized())
                {
                    await Task.Delay(50).ConfigureAwait(false);
                }
            }

            // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx
            if (workaround)
            {
                // Simulate an "ALT" key press, make it double to remove menu activation
                InputGenerator.KeyPress(VirtualKeyCodes.MENU, VirtualKeyCodes.MENU);
            }
            // Show window in forground.
            User32Api.BringWindowToTop(interopWindow.Handle);
            User32Api.SetForegroundWindow(interopWindow.Handle);
        }
Esempio n. 4
0
 private void prepare()
 {
     WriteLine("3");
     Thread.Sleep(1000);
     WriteLine("2");
     Thread.Sleep(1000);
     WriteLine("1");
     Thread.Sleep(1000);
     InputGenerator.KeyPress(VirtualKeyCodes.KEY_K);
     Thread.Sleep(233);
 }
Esempio n. 5
0
        /// <summary>
        ///     Move to the start
        /// </summary>
        /// <returns>bool if this worked</returns>
        public bool Start()
        {
            var        result = false;
            ScrollInfo scrollInfoBefore;
            var        hasScrollInfo = TryRetrievePosition(out scrollInfoBefore);

            switch (ScrollMode)
            {
            case ScrollModes.KeyboardPageUpDown:
                InputGenerator.KeyDown(VirtualKeyCodes.CONTROL);
                InputGenerator.KeyPress(VirtualKeyCodes.HOME);
                InputGenerator.KeyUp(VirtualKeyCodes.CONTROL);
                result = true;
                break;

            case ScrollModes.WindowsMessage:
                result = SendScrollMessage(ScrollBarCommands.SB_TOP);
                break;

            case ScrollModes.AbsoluteWindowMessage:
                result = hasScrollInfo;
                if (hasScrollInfo)
                {
                    // Calculate start position, clone the scrollInfoBefore
                    var scrollInfoForStart = scrollInfoBefore;
                    scrollInfoForStart.Position = scrollInfoBefore.Minimum;
                    result = ApplyPosition(ref scrollInfoForStart);
                }
                break;

            case ScrollModes.MouseWheel:
                result = true;
                while (!IsAtStart)
                {
                    if (!Previous())
                    {
                        break;
                    }
                }
                break;
            }
            return(result);
        }
Esempio n. 6
0
        public void SendInput()
        {
            switch (Type)
            {
            case KeyType.KeyDown:
                InputGenerator.KeyDown((VirtualKeyCodes)(Num + 48));
                Debug.WriteLine("Pushed" + Num.ToString());
                break;

            case KeyType.KeyUp:
                InputGenerator.KeyUp((VirtualKeyCodes)(Num + 48));
                Debug.WriteLine("Released " + Num.ToString());
                break;

            case KeyType.KeyPress:
                InputGenerator.KeyPress((VirtualKeyCodes)(Num + 48));
                Debug.WriteLine("Pressed" + Num.ToString());
                break;
            }
        }
Esempio n. 7
0
        /// <summary>
        ///     Test scrolling a window
        /// </summary>
        /// <returns></returns>
        //[StaFact]
        private async Task TestScrollingAsync()
        {
            var breakScroll = false;

            IDisposable keyboardhook = null;

            try
            {
                keyboardhook = KeyboardHook.KeyboardEvents.Where(args => args.Key == VirtualKeyCodes.ESCAPE).Subscribe(args => breakScroll = true);
                // Start a process to test against
                using (var process = Process.Start("notepad.exe", "C:\\Windows\\setupact.log"))
                {
                    // Make sure it's started
                    Assert.NotNull(process);
                    // Wait until the process started it's message pump (listening for input)
                    process.WaitForInputIdle();

                    try
                    {
                        // Find the belonging window, by the process id
                        var notepadWindow = WindowsEnumerator.EnumerateWindows()
                                            .FirstOrDefault(interopWindow =>
                        {
                            int processId;
                            User32Api.GetWindowThreadProcessId(interopWindow.Handle, out processId);
                            return(processId == process.Id);
                        });
                        Assert.NotNull(notepadWindow);

                        // Create a WindowScroller
                        var scroller = notepadWindow.GetChildren().Select(window => window.GetWindowScroller(ScrollBarTypes.Vertical)).FirstOrDefault();

                        Assert.NotNull(scroller);
                        // Notepad should have ScrollBarInfo
                        scroller.GetScrollbarInfo();
                        Assert.True(scroller.ScrollBar.HasValue);

                        Log.Info().WriteLine("Scrollbar info: {0}", scroller.ScrollBar.Value);

                        User32Api.SetForegroundWindow(scroller.ScrollingWindow.Handle);
                        await Task.Delay(1000);

                        // Just make sure the window is changed
                        InputGenerator.KeyPress(VirtualKeyCodes.NEXT, VirtualKeyCodes.DOWN);
                        await Task.Delay(2000);

                        scroller.ScrollMode  = ScrollModes.WindowsMessage;
                        scroller.ShowChanges = false;
                        // Move the window to the start
                        Assert.True(scroller.Start());
                        // A delay to make the window move
                        await Task.Delay(2000);

                        // Check if it did move to the start
                        Assert.True(scroller.IsAtStart);

                        // Loop
                        do
                        {
                            if (breakScroll)
                            {
                                break;
                            }
                            // Next "page"
                            Assert.True(scroller.Next());
                            // Wait a bit, so the window can update
                            await Task.Delay(300);

                            // Loop as long as we are not at the end yet
                        } while (!scroller.IsAtEnd);
                        scroller.Reset();
                    }
                    finally
                    {
                        // Kill the process
                        process.Kill();
                    }
                }
            }
            finally
            {
                keyboardhook?.Dispose();
            }
        }