0

I have a PuppeteerSharp application which does some basic browser automation. The application cycle should look like this:

  1. Launch browser (Chrome)
  2. Get search term
  3. open url 'google.ch'
  4. enter search term in search input
  5. press enter
  6. Wait for left-arrow key or right-arrow key press in any application (other key presses should not have any impact)
  7. If left-arrow key pressed, then do some logic
  8. Else if right-arrow key pressed, then do some logic

My problem starts with the step at number 6. I can't find a way to wait for left-arrow key or right-arrow key press.

Here is my current code with some pseudo code, for how i expect the "key press wait" logic to look like:

using System.Threading.Tasks;
using PuppeteerSharp;

namespace GoogleMapsChecker
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            // launch browser and save in variable
            var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = false,
                ExecutablePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe" // get path to chrome executable
            });

            var page = await browser.NewPageAsync();
            var searchTerm = "searchTerm";

            await page.GoToAsync("https://www.google.ch/");
            await Task.Delay(5000);
            //Click on google search box
            await page.ClickAsync(".gLFyf");
            await page.Keyboard.SendCharacterAsync(searchTerm);
            await page.Keyboard.PressAsync("Enter");

            Wait for KeyPress(LeftArrow || RightArrow)
            {
                if (KeyPress(LeftArrow))
                {
                    //do stuff
                }
                else if (KeyPress(RightArrow))
                {
                    //do other stuff
                }
            }
        }
    }
}

How can i wait until specific keys a pressed and then depending on pressed key act further?

UPDATE: Some of you noted similair questions which are for console applications. My application is a console application but i want to detect the keypress even when my program isn't in focus. I tried the answers from the refered question but it didn't solve my problem.

Nightscape
  • 464
  • 6
  • 19
  • 1
    Is it a console application? Do you want to detect the keypress when your program is in focus or even if it's not? – Magnetron Jun 15 '21 at 13:18
  • 1
    Does this answer your question? [C# arrow key input for a console app](https://stackoverflow.com/questions/4351258/c-sharp-arrow-key-input-for-a-console-app) – Martheen Jun 15 '21 at 13:18
  • something like [listen-for-key-press-in-net-console-app](https://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app/5891596) ? – Mong Zhu Jun 15 '21 at 13:19
  • This may help: [How do I detect keyPress while not focused?](https://stackoverflow.com/questions/18291448/how-do-i-detect-keypress-while-not-focused) – Magnetron Jun 15 '21 at 13:38
  • @Magnetron i will take a look at it right now, thanks – Nightscape Jun 15 '21 at 13:39

0 Answers0