I have a PuppeteerSharp application which does some basic browser automation. The application cycle should look like this:
- Launch browser (Chrome)
- Get search term
- open url 'google.ch'
- enter search term in search input
- press enter
- Wait for left-arrow key or right-arrow key press in any application (other key presses should not have any impact)
- If left-arrow key pressed, then do some logic
- 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.